question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

timedelta64[D] is always coerced to timedelta64[ns]

See original GitHub issue

Hi 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:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
hottwajcommented, Dec 1, 2016

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

dates = pandas.Series([datetime.date(2016, 01, 10), datetime.date(2016, 01, 20), datetime.date(2016, 01, 25)]).astype('datetime64[ns]')

dates.diff().astype('timedelta64[D]').astype(float)
#returns 
#0     NaN
#1    10.0
#2     5.0
#dtype: float6

xarray.DataArray(dates).diff(dim = 'dim_0').astype('timedelta64[D]').astype(float)
#returns
#<xarray.DataArray (dim_0: 2)>
#array([  8.64000000e+14,   4.32000000e+14])
#Coordinates:
#  * dim_0    (dim_0) int64 1 2

Again the xarray result is in ns rather than days.

Thanks

0reactions
shoyercommented, Aug 30, 2018

df_trans['DELTA'].dt.days should work, in both pandas in xarray.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found