conversion of pandas Timestamps to numpy datetime64[ns] looses nanoseconds
See original GitHub issueThere 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:
- Created 4 years ago
- Comments:5 (5 by maintainers)
Top 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 >
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 Free
Top 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

I think the resolution was (and is) correct, but
__repr__has recently been fixed to correctly show nanoseconds on that case.Agreed. A PR to do this would be welcome in pandas.