timedelta64[D] is always coerced to timedelta64[ns]
See original GitHub issueHi guys, the following snippets show the issue…
xarray.DataArray([1,2,3,4]).astype('timedelta64[D]')
#output is
"""
<xarray.DataArray (dim_0: 4)>
array([ 86400000000000, 172800000000000, 259200000000000, 345600000000000], dtype='timedelta64[ns]')
Coordinates:
* dim_0 (dim_0) int64 0 1 2 3
"""
Compare this with Pandas:
pandas.Series([1,2,3,4]).astype('timedelta64[D]')
#output is
"""
0 1 days
1 2 days
2 3 days
3 4 days
dtype: timedelta64[D]
"""
This behvaiour becomes more problematic when trying to convert from timedelta[ns] to e.g. days as ints:
xarray.DataArray(pandas.Series([1,2,3,4]).astype('timedelta64[D]')).astype(int)
#output is
"""
<xarray.DataArray (dim_0: 4)>
array([ 86400000000000, 172800000000000, 259200000000000, 345600000000000])
Coordinates:
* dim_0 (dim_0) int64 0 1 2 3
"""
Again contrast that with pandas:
pandas.Series([1,2,3,4]).astype('timedelta64[D]').astype(int)
#output is
"""
0 1
1 2
2 3
3 4
dtype: int64
"""
Other variations of timedelta e.g. timedelta64[s], timedelta64[W] etc suffer from the same problem.
Thanks
Issue Analytics
- State:
- Created 7 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
dtype timedelta64[ns] cannot be converted to datetime64[ns]
The problem is that a standalone time cannot be a datetime - it doesn't have a date - so pandas imports it as...
Read more >Datetimes and Timedeltas — NumPy v1.24 Manual
The Datetime and Timedelta data types support a large number of time units, as well as generic units which can be coerced into...
Read more >Time Series / Date functionality — pandas 0.23.0 documentation
Using the NumPy datetime64 and timedelta64 dtypes, we have consolidated a large ... Lists of Timestamp and Period are automatically coerced to DatetimeIndex ......
Read more >Working with Time Series | Python Data Science Handbook
One detail of the datetime64 and timedelta64 objects is that they are built on a ... in previous sections, passing values that can...
Read more >Pandas Training – Building Skills for Data Science
to_numpy() will always return a NumPy array, potentially at the cost of copying / coercing values. When your DataFrame contains a mixture of...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The pandas docs do seem to say that conversion to timedelta64[D] (or other frequencies) is possible - see: http://pandas.pydata.org/pandas-docs/stable/timedeltas.html#frequency-conversion
Also here’s a more realistic example of why this is problematic for me - I have a sequence of dates and I want to calculate the difference between them in days: possible in pandas, but not possible in xarray without first reverting to pandas/numpy types
Again the xarray result is in ns rather than days.
Thanks
df_trans['DELTA'].dt.days
should work, in both pandas in xarray.