Skip to content

ENH/API: preserve non-nano in to_datetime #50369

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

Merged
merged 16 commits into from
Jan 9, 2023

Conversation

jbrockmendel
Copy link
Member

  • closes #xxxx (Replace xxxx with the GitHub issue number)
  • Tests added and passed if fixing a bug or adding a new feature
  • All code checks passed.
  • Added type annotations to new arguments/methods/functions.
  • Added an entry in the latest doc/source/whatsnew/vX.X.X.rst file if fixing a bug or adding a new feature.

@MarcoGorelli MarcoGorelli self-requested a review December 25, 2022 22:45
if not is_supported_unit(get_unit_from_dtype(arg_dtype)):
# We go to closest supported reso, i.e. "s"
arg = astype_overflowsafe(
np.asarray(arg), np.dtype("M8[s]"), coerce=errors == "coerce"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is errors="ignore" handled?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will continue to incorrectly raise for the time being

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha. Planning on addressing in this PR or worth adding a TODO/xfail test?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will add a TODO in my next "assorted" branch if this isn't a show-stopper

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(actually looks like this needs rebase so will add comment here)

@mroeschke mroeschke added the Non-Nano datetime64/timedelta64 with non-nanosecond resolution label Dec 27, 2022
Copy link
Member

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice! just got a question about coverage

@@ -304,6 +304,7 @@ cpdef ndarray astype_overflowsafe(
cnp.dtype dtype,
bint copy=True,
bint round_ok=True,
bint coerce=False,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps is_coerce, for consistency with tslib?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

Comment on lines +531 to +534
if is_datetime64_dtype(bins):
# As of 2.0, to_datetime may give non-nano, so we need to convert
# here until the rest of this file recognizes non-nano
bins = bins.astype("datetime64[ns]", copy=False)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there any test that would hit the else branch? I think now is_datetime64_dtype(bins) is always True in the test suite

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no extant test, but im not sure its impossible to reach

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps raise an AssertionError in the else block? the it won't show up as uncovered

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

im not concerned about it showing as uncovered, and don't want to break potentially-working code in the wild.

will need to revisit this eventually to handle non-nano without the cast anyway

@@ -858,22 +858,31 @@ def test_to_datetime_dt64s_and_str(self, arg, format):
"dt", [np.datetime64("1000-01-01"), np.datetime64("5000-01-02")]
)
def test_to_datetime_dt64s_out_of_bounds(self, cache, dt):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should the name of the test change, now that they're no longer out-of-bounds?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gentle reminder

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jbrockmendel last one, sorry 😄

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

already renamed to reflect it is only out of ns bounds

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed that, thanks!

Comment on lines 862 to 870
ts = to_datetime(dt, errors="raise", cache=cache)
assert isinstance(ts, Timestamp)
assert ts.unit == "s"
assert ts.asm8 == dt

ts = to_datetime(dt, errors="coerce", cache=cache)
assert isinstance(ts, Timestamp)
assert ts.unit == "s"
assert ts.asm8 == dt
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is checking with both raise and coerce necessary, given that no errors are thrown in this test anyway? errors='ignore' works too

could we either parametrise over that too, or just remove errors=?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gentle reminder

Copy link
Member

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the code changes are fine, I'd just left a couple of comments (which may have got lost in the conversation), so I've just bumped them

Comment on lines 862 to 870
ts = to_datetime(dt, errors="raise", cache=cache)
assert isinstance(ts, Timestamp)
assert ts.unit == "s"
assert ts.asm8 == dt

ts = to_datetime(dt, errors="coerce", cache=cache)
assert isinstance(ts, Timestamp)
assert ts.unit == "s"
assert ts.asm8 == dt
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gentle reminder

@@ -858,22 +858,31 @@ def test_to_datetime_dt64s_and_str(self, arg, format):
"dt", [np.datetime64("1000-01-01"), np.datetime64("5000-01-02")]
)
def test_to_datetime_dt64s_out_of_bounds(self, cache, dt):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gentle reminder

Copy link
Member

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me pending green

@mroeschke mroeschke added this to the 2.0 milestone Jan 9, 2023
@mroeschke mroeschke merged commit ef0eaa4 into pandas-dev:main Jan 9, 2023
@mroeschke
Copy link
Member

Thanks @jbrockmendel

@jbrockmendel jbrockmendel deleted the ref-to_datetime-2 branch January 9, 2023 23:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Non-Nano datetime64/timedelta64 with non-nanosecond resolution
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants