Skip to content
Advertisement

extracting days from a numpy.timedelta64 value

I am using pandas/python and I have two date time series s1 and s2, that have been generated using the ‘to_datetime’ function on a field of the df containing dates/times.

When I subtract s1 from s2

s3 = s2 – s1

I get a series, s3, of type

timedelta64[ns]

JavaScript

How do I look at 1 element of the series:

s3[10]

I get something like this:

numpy.timedelta64(2069211000000000,’ns’)

How do I extract days from s3 and maybe keep them as integers(not so interested in hours/mins etc.)?

Advertisement

Answer

You can convert it to a timedelta with a day precision. To extract the integer value of days you divide it with a timedelta of one day.

JavaScript

Or, as @PhillipCloud suggested, just days.astype(int) since the timedelta is just a 64bit integer that is interpreted in various ways depending on the second parameter you passed in ('D', 'ns', …).

You can find more about it here.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement