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

BUG: Series(..) coerces datetime64[non-ns] array differently depending on writable flag #34843

Closed
jorisvandenbossche opened this issue Jun 17, 2020 · 3 comments · Fixed by #34844
Labels
API - Consistency Internal Consistency of API/Behavior Bug Datetime Datetime data dtype
Milestone

Comments

@jorisvandenbossche
Copy link
Member

Normally, if we create a Series or DataFrame from a numpy array of non-ns datetime64, and the values are within the ns-range, we convert to datetime64[ns] (in sanitize_array).

However, this does not seem to happen if the array is not writeable:

In [9]: arr = np.array(['2020-01-01T00:00:00.000000'], dtype="datetime64[us]") 

In [10]: pd.Series(arr) 
Out[10]: 
0   2020-01-01
dtype: datetime64[ns]

In [11]: arr.flags.writeable = False  

In [12]: pd.Series(arr)  
Out[12]: 
0    2020-01-01 00:00:00
dtype: object

I suppose this is a bug? (@jbrockmendel ?)

It seems this is due to tslibs.conversion.ensure_datetime64ns failing on a read-only buffer, and it started "failing" since pands 0.25

@jorisvandenbossche
Copy link
Member Author

Fix: #34844. This was caused by #22147 (we really should be more careful with memoryviews, although I suppose we nowadays are).
I quickly skimmed that PR, and most changes there have been fixed in the mean-time (either converted back to use ndarray or added const), a few are remaining, like:

array_strptime (and also array_to_timedelta64 -> to_timedelta)

In [4]: arr = np.array(['15/10/2020'], dtype=object) 

In [5]: arr.flags.writeable = False   

In [6]: pd.to_datetime(arr, format="%d/%m/%Y")    
...
ValueError: buffer source array is read-only

and then a few in parsing.pyx, but I suppose those are generally not a problem, if the arrays are coming from out parsing code, they will never be read-only.

@jbrockmendel
Copy link
Member

It seems this is due to tslibs.conversion.ensure_datetime64ns failing on a read-only buffer,

I think this is right. Best guess is this can be fixed by changing the line int64_t[:] ivalues, iresult to two lines const int64_t[:] ivalues, int64_t[:] iresult

@jorisvandenbossche
Copy link
Member Author

Best guess is this can be fixed by changing the line ..

Indeed, already did that ;) -> #34844

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API - Consistency Internal Consistency of API/Behavior Bug Datetime Datetime data dtype
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants