1

The date series is something like this:

df.d_num_active.head()

# result
0   191 days
1    49 days
2   273 days
3   287 days
4   634 days
Name: d_num_active, dtype: timedelta64[ns]

The problem:

>>> df.d_num_active.head().astype('timedelta64[D]')

ValueError: Cannot convert from timedelta64[ns] to timedelta64[D].
Supported resolutions are 's', 'ms', 'us', 'ns'`

The question is:

one the one hand, it's seemingly supposed to be OK according to this answer and this answer.

on the other hand dt.days works:

df.d_num_active.head().dt.days

# result
0    191
1     49
2    273
3    287
4    634
Cornelius Roemer
  • 3,772
  • 1
  • 24
  • 55
Sean.H
  • 640
  • 1
  • 6
  • 18
  • 1
    Just to emphasize: `dt.days` gives you a type int, not timedelta64[D]. Those are not the same. – FObersteiner Apr 27 '23 at 11:19
  • @FObersteiner, great! – Sean.H Apr 27 '23 at 15:03
  • 1
    @jezrael: I don't fully understand how this is a dupe; the answer to the question you linked suggests `df["month_15"].values.astype('datetime64[D]')` - that also won't work with the latest version of pandas. Again, the minimum precision is `seconds` (for datetime and timedelta type) – FObersteiner Apr 27 '23 at 15:15
  • @FObersteiner I agree, pandas v2 deprecates the accepted answer of the dupe. So maybe one should create a question asking specifically how to upgrade from v1 to v2. – Cornelius Roemer May 30 '23 at 10:19
  • @CorneliusRoemer since the linked dupe is about how to use a datetime series within a numba function: personally, I'd recommend using a numeric representation of that datetime series (e.g. Unix time) in the first place (i.e. pass that to the numba jit-compiled func). As long as you stay out of the time zone territory, this is pretty straight-forward and allows you to work with simple integers :) – FObersteiner May 30 '23 at 18:53

1 Answers1

1

This is not supported by pandas.

If you need to convert to timedelta64[D], use a numpy array:

df['d_num_active'].to_numpy().astype('timedelta64[D]')

# array([191,  49, 273, 287, 634], dtype='timedelta64[D]')

If you try to convert back to Series, this will force one of the supported types:

pd.Series(df['d_num_active'].to_numpy().astype('timedelta64[D]'))

0   191 days 00:00:00
1    49 days 00:00:00
2   273 days 00:00:00
3   287 days 00:00:00
4   634 days 00:00:00
dtype: timedelta64[s]



pd.to_timedelta(df['d_num_active'].dt.days, unit='D')

0   191 days
1    49 days
2   273 days
3   287 days
4   634 days
Name: d_num_active, dtype: timedelta64[ns]
mozway
  • 194,879
  • 13
  • 39
  • 75