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.

conversion of pandas Timestamps to numpy datetime64[ns] looses nanoseconds

See original GitHub issue

There seems to be a problem when converting pandas Timestamps at ‘ns’ resolution into numpy datetime64[ns] arrays…

The following lines show that the nanosecond values are lost in the conversion! I’m not sure if the problem is with pandas or numpy but it results in some annoying problems like the fact that you can not search for a converted value in case the nanoseconds are !=0

Reproducing code example:

import numpy as np
import pandas as pd

x = pd.Timestamp('2017-01-01 09:58:50.624025600', unit='ns')
y = np.array(x, dtype=np.dtype('datetime64[ns]'))
y2 = np.array(x.value, dtype=np.dtype('datetime64[ns]'))

print(x) # the original pandas Timestamp
>>> '2017-01-01 09:58:50.624025600'
print(y)
>>> '2017-01-01T09:58:50.624025000'
print(y2)
>>> '2017-01-01T09:58:50.624025600'

print(y - y2):
>>> 'numpy.timedelta64(-600,'ns')'

Numpy/Python version information:

1.18.1 3.6.7 (default, Dec 6 2019, 07:03:06) [MSC v.1900 64 bit (AMD64)]

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jbrockmendelcommented, May 8, 2020

Note that the pandas Timestamp has a resolution attribute which does not extend to ns, even when the stored value contains nanoseconds. From the above example:

I think the resolution was (and is) correct, but __repr__ has recently been fixed to correctly show nanoseconds on that case.

0reactions
jbrockmendelcommented, Feb 16, 2021

Perhaps the fix here is for Timestamp to implement array

Agreed. A PR to do this would be welcome in pandas.

Read more comments on GitHub >

github_iconTop Results From Across the Web

pandas.Timestamp.to_numpy — pandas 1.5.2 documentation
Convert the Timestamp to a NumPy datetime64. New in version 0.25.0. This is an alias method for Timestamp.to_datetime64() . The dtype and copy...
Read more >
How to get unix timestamp from numpy.datetime64
Note that this converts to 'seconds' (the [s] ) prior to converting to integers. This works on NumPy 1.12.1. Share.
Read more >
[#ARROW-7747] [Python] coerce_timestamps + ... - Apache
... using pyarrow==0.15.1 pandas==0.25.3 numpy==1.18.1 I'm trying to write a table containing nanosecond timestamps to a millisecond schema.
Read more >
10 Tricks for Converting Numbers and Strings to Datetime in ...
In Pandas, missing values are given the value NaN , short for “Not a Number”. df = pd.DataFrame({'date': ['3/10/2000', np.nan, '3/12/ ...
Read more >
Datetimes and Timedeltas — NumPy v1.25.dev0 Manual
An explicit support for TAI and TAI to UTC conversion, accounting for leap ... the time units are hours ('h'), minutes ('m'), seconds...
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