Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix period range #5393

Merged
merged 3 commits into from
Aug 17, 2022
Merged

Fix period range #5393

merged 3 commits into from
Aug 17, 2022

Conversation

hoxbro
Copy link
Member

@hoxbro hoxbro commented Aug 17, 2022

Fixes #5392

@jlstevens
Copy link
Contributor

jlstevens commented Aug 17, 2022

Thanks for the fix!

Looks good to me though the new unit test is currently failing with:

    def test_dataset_range_for_pd_period_range(self):
        period_range = pd.period_range("1990", "1991", freq="M")
        expected_range = (pd.Timestamp('1990-01-01 00:00:00'), pd.Timestamp('1991-01-01 00:00:00'))
        curve = Curve((period_range, range(13)))
>       assert curve.range("x") == expected_range
E       AssertionError: assert (Period('1990...991-01', 'M')) == (Timestamp('1...01 00:00:00'))
E         At index 0 diff: Period('1990-01', 'M') != Timestamp('1990-01-01 00:00:00')
E         Full diff:
E         - (Timestamp('1990-01-01 00:00:00'), Timestamp('1991-01-01 00:00:00'))
E         + (Period('1990-01', 'M'), Period('1991-01', 'M'))

Looks like you just need to use Period objects instead of pd.Timestamp here....

Happy to merge once the tests are green.

@hoxbro
Copy link
Member Author

hoxbro commented Aug 17, 2022

After thinking about it, I think the correct fix is in max_range and not in the PandasInterface.range.

Before my last commit, it would return (np.nan, np.nan) because this raises a TypeError pd.to_datetime(pd.Period("1990", freq="M"))

TypeError                                 Traceback (most recent call last)
Input In [3], in <cell line: 1>()
----> 1 pd.to_datetime(pd.Period("1990", freq="M"))

File ~/miniconda3/envs/holoviz/lib/python3.9/site-packages/pandas/core/tools/datetimes.py:1078, in to_datetime(arg, errors, dayfirst, yearfirst, utc, format, exact, unit, infer_datetime_format, origin, cache)
   1076         result = convert_listlike(arg, format)
   1077 else:
-> 1078     result = convert_listlike(np.array([arg]), format)[0]
   1080 #  error: Incompatible return value type (got "Union[Timestamp, NaTType,
   1081 # Series, Index]", expected "Union[DatetimeIndex, Series, float, str,
   1082 # NaTType, None]")
   1083 return result

File ~/miniconda3/envs/holoviz/lib/python3.9/site-packages/pandas/core/tools/datetimes.py:402, in _convert_listlike_datetimes(arg, format, name, tz, unit, errors, infer_datetime_format, dayfirst, yearfirst, exact)
    400 assert format is None or infer_datetime_format
    401 utc = tz == "utc"
--> 402 result, tz_parsed = objects_to_datetime64ns(
    403     arg,
    404     dayfirst=dayfirst,
    405     yearfirst=yearfirst,
    406     utc=utc,
    407     errors=errors,
    408     require_iso8601=require_iso8601,
    409     allow_object=True,
    410 )
    412 if tz_parsed is not None:
    413     # We can take a shortcut since the datetime64 numpy array
    414     # is in UTC
    415     dta = DatetimeArray(result, dtype=tz_to_dtype(tz_parsed))

File ~/miniconda3/envs/holoviz/lib/python3.9/site-packages/pandas/core/arrays/datetimes.py:2224, in objects_to_datetime64ns(data, dayfirst, yearfirst, utc, errors, require_iso8601, allow_object, allow_mixed)
   2222 order: Literal["F", "C"] = "F" if flags.f_contiguous else "C"
   2223 try:
-> 2224     result, tz_parsed = tslib.array_to_datetime(
   2225         data.ravel("K"),
   2226         errors=errors,
   2227         utc=utc,
   2228         dayfirst=dayfirst,
   2229         yearfirst=yearfirst,
   2230         require_iso8601=require_iso8601,
   2231         allow_mixed=allow_mixed,
   2232     )
   2233     result = result.reshape(data.shape, order=order)
   2234 except ValueError as err:

File ~/miniconda3/envs/holoviz/lib/python3.9/site-packages/pandas/_libs/tslib.pyx:381, in pandas._libs.tslib.array_to_datetime()

File ~/miniconda3/envs/holoviz/lib/python3.9/site-packages/pandas/_libs/tslib.pyx:613, in pandas._libs.tslib.array_to_datetime()

File ~/miniconda3/envs/holoviz/lib/python3.9/site-packages/pandas/_libs/tslib.pyx:755, in pandas._libs.tslib._array_to_datetime_object()

File ~/miniconda3/envs/holoviz/lib/python3.9/site-packages/pandas/_libs/tslib.pyx:586, in pandas._libs.tslib.array_to_datetime()

TypeError: <class 'pandas._libs.tslibs.period.Period'> is not convertible to datetime

@codecov-commenter
Copy link

codecov-commenter commented Aug 17, 2022

Codecov Report

Merging #5393 (f861ad2) into master (8b2ee78) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #5393   +/-   ##
=======================================
  Coverage   88.07%   88.07%           
=======================================
  Files         301      301           
  Lines       61969    61972    +3     
=======================================
+ Hits        54579    54582    +3     
  Misses       7390     7390           
Impacted Files Coverage Δ
holoviews/core/data/pandas.py 93.30% <ø> (-0.10%) ⬇️
holoviews/core/util.py 86.06% <100.00%> (+0.03%) ⬆️
holoviews/tests/core/test_utils.py 99.20% <100.00%> (+<0.01%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@philippjfr
Copy link
Member

Looks good to me!

@philippjfr philippjfr merged commit 89368a2 into master Aug 17, 2022
@philippjfr philippjfr deleted the fix_period_range branch August 17, 2022 14:31
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

period indexed data frame causes time axis range to be incorrect
4 participants