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

comparing vs. subtracting different calendars #236

Closed
mathause opened this issue Mar 26, 2021 · 6 comments
Closed

comparing vs. subtracting different calendars #236

mathause opened this issue Mar 26, 2021 · 6 comments

Comments

@mathause
Copy link

The following snipped used to raise a TypeError. With the changed behavior of __richcompare__ this now raises a ValueError in __sub__ which pandas ignores:

import pandas as pd
import cftime

time_1 = cftime.DatetimeGregorian(2000, 1, 1)
time_2 = cftime.DatetimeProlepticGregorian(2001, 1, 1)

pd.Series([time_1, time_2]).rank(method="dense", numeric_only=False)

This is used in xarray to avoid combining datasets with different calendars, which does not really work anymore with this change. I don't know why pandas ignores the error (and it's not silent). So it's not really a cftime issue but I thought I report it here anyway.

  • Do you plan to allow __sub__ between different calendars?
  • Should __sub__ raise a TypeError instead of a ValueError
@jswhit
Copy link
Collaborator

jswhit commented Mar 26, 2021

PR #234 enabled __richcmp__ between different calendars. You are right that the error should be changed to a TypeError. I will create a PR for that. As far as enabling __sub__ between different calendars, that's a tricky one. It's possible, but which calendar type would be returned? PR #234 also added a change_calendar method that can be used to achieve this - I think it's probably better to force users to explicity do d1 - d2.change_calendar(d1.calendar).

jswhit added a commit that referenced this issue Mar 26, 2021
Change ValueError to TypeError in datetime.__sub__ (closes issue #236)
@jswhit
Copy link
Collaborator

jswhit commented Mar 26, 2021

PR #236 merged. @mathause please let us know whether that fixes the xarray tests

@spencerkclark
Copy link
Collaborator

Great, thanks @jswhit -- that indeed fixed the issue @mathause identified. The removal of utime (#235) resulted in some nc-time-axis-related test failures in xarray as well (it appears nc-time-axis makes extensive use of it).

It was fairly straightforward to remove nc-time-axis's use of utime (see my updates to jswhit/nc-time-axis#1). Do you think we should try and get SciTools/nc-time-axis#51 merged there? It sounds like they would make a new release after that: SciTools/nc-time-axis#54 (comment).

@mathause
Copy link
Author

mathause commented Mar 28, 2021

Unfortunately it did not work. Actually pandas ignores the TypeError silently 🤦

https://github.com/pandas-dev/pandas/blob/029907c9d69a0260401b78a016a6c4515d8f1c40/pandas/_libs/algos.pyx#L87

Still I think TypeError is the right one to raise - so I guess we have to fix this in xarray & am not sure how to best do this. Pity, the old version was quite elegant.


Edit: we can probably do

pd.Series([time_1, time_2]).diff()

which will raise the appropriate error.

@spencerkclark
Copy link
Collaborator

spencerkclark commented Mar 28, 2021

Oops, I don't know how I missed that that test still failed in the xarray test suite...thanks @mathause for correcting me.

I agree we can work around this in xarray.

@mathause
Copy link
Author

mathause commented Apr 1, 2021

I think from my side this can be closed - thanks @jswhit & @spencerkclark

@jswhit jswhit closed this as completed Apr 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants