-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BUG: Timestamp constructor ignores unit parameter value #53198
Comments
I tracked this to this section of timestamps.pyx:
We can see that unit is explicitly set to None, presumably because we are using datetime which does not have precision greater than seconds. This is then passed to get_supported_reso. This would indicate that the return is ns but we observe us.
I may be looking in the wrong direction but hoping this helps others pinpoint more accurately. |
This is interesting, this behavior does not happen happen when passing the date components via keyword instead of by position. Will continue down that track:
|
I think I found part of the issue, the following piece of code in
Still need to figure out what is the resolution for this bug. |
take |
I think I better understand the situation now. @georgipeev passing a year, month and date creates a python If you specify nanoseconds when creating your timestamp it will have |
@alexprincel Thanks for investigating the underlying reason of this behavior. |
@georgipeev I'll play around with substituting the python |
How is this still an issue almost a year later? If Upgrading from pandas 2.0 to 2.1 has been quite a pain to deal with the timestamp inconsistencies. |
I was also bit by this. Not sure if it's a separate issue/intended, but there is also a difference when passing a string vs. integer arguments: >>> pd.__version__
'2.1.1'
>>> pd.Timestamp("2022-01-31", unit="ns").unit
's'
>>> pd.Timestamp("2022-01-31").unit
's'
>>> pd.Timestamp(year=2022, month=1, day=31).unit
'us'
>>> pd.Timestamp(year=2022, month=1, day=31, unit="ns").unit
'us' |
This seems to only be true if one specifies a non-zero >>> pd.Timestamp(year=2022, month=1, day=31, nanosecond=0).unit
'us'
>>> pd.Timestamp(year=2022, month=1, day=31, nanosecond=1).unit
'ns' but not always... >>> t = pd.Timestamp("2022-01-31")
>>> t = t.replace(nanosecond=1)
>>> t
Timestamp('2022-01-31 00:00:00.000000001')
>>> t.unit
's' |
Is anyone working on this? Can I work on it? |
Pandas version checks
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
Issue Description
It seems that in Pandas 2.0 the
Timestamp
constructor ignores the value of theunit
parameter and always initializes the instance tounit='us'
.The unit of a
Timestamp
can, however, be changed correctly via theas_unit
method.Expected Behavior
Installed Versions
INSTALLED VERSIONS
commit : 37ea63d
python : 3.9.16.final.0
python-bits : 64
OS : Linux
OS-release : 4.18.0-348.23.1.el8_5.jump2.x86_64
Version : #1 SMP Tue Nov 1 14:32:55 CDT 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 2.0.1
numpy : 1.22.4
pytz : 2022.2.1
dateutil : 2.8.2
setuptools : 66.0.0
pip : 23.1.2
Cython : 0.29.32
pytest : 7.3.1
hypothesis : None
sphinx : 7.0.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.1
html5lib : 1.1
pymysql : 1.0.2
psycopg2 : None
jinja2 : 3.1.2
IPython : None
pandas_datareader: None
bs4 : 4.11.2
bottleneck : None
brotli : None
fastparquet : None
fsspec : 2023.5.0
gcsfs : None
matplotlib : None
numba : 0.56.0
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 12.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.10.1
snappy : None
sqlalchemy : 1.4.40
tables : None
tabulate : 0.8.10
xarray : None
xlrd : None
zstandard : 0.21.0
tzdata : 2022.2
qtpy : None
pyqt5 : None
The text was updated successfully, but these errors were encountered: