Skip to content

Error in pd.to_datetime, with argument np.datetime64 array of 51 elements #31491

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

Closed
pajachiet opened this issue Jan 31, 2020 · 4 comments · Fixed by #31520
Closed

Error in pd.to_datetime, with argument np.datetime64 array of 51 elements #31491

pajachiet opened this issue Jan 31, 2020 · 4 comments · Fixed by #31520
Labels
Datetime Datetime data dtype Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@pajachiet
Copy link
Contributor

Code Sample, a copy-pastable example if possible

import pandas as pd
import numpy as np
d = np.datetime64("2000-01-01", "s")

pd.to_datetime(np.full(50, d))
# Works

pd.to_datetime(np.full(51,  d))
# ERROR
# pandas._libs.tslibs.np_datetime.OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 29999249045-07-29 00:00:00



pd.to_datetime(pd.Series(np.full(51,  d)))

Problem description

With pandas 1.0, we get a new issue when using pd.to_datetime to convert an existing np.datetime64 array with 51 elements.

This works with 50 elements, or when converting to a Series beforehand, so I believe the problems comes from inside implicit conversion.

@jorisvandenbossche jorisvandenbossche added Regression Functionality that used to work in a prior pandas version Datetime Datetime data dtype labels Jan 31, 2020
@jorisvandenbossche jorisvandenbossche added this to the 1.0.1 milestone Jan 31, 2020
@jorisvandenbossche
Copy link
Member

@pajachiet Thanks for the report!

@TomAugspurger
Copy link
Contributor

The difference between 50 and 51 is from the caching we do in to_datetime

if check_count is None:
# in this case, the gain from caching is negligible
if len(arg) <= 50:
return False

@TomAugspurger
Copy link
Contributor

The bug is in unique

In [20]: d = np.array([np.datetime64("2000-01-01", "s")])

In [21]: pd.unique(d)
Out[21]: array(['29999249045-07-29T00:00:00'], dtype='datetime64[s]')

So we should either handle non-ns datetimes there, or convert to ns before calling it.

@TomAugspurger
Copy link
Contributor

Still working on this. Getting closer, but turned up a second issue with errors='coerce' and OOB timestamps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Datetime Datetime data dtype Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants