Skip to content

BUG: int origin treated as nanoseconds since epoch time #54788

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

Open
2 of 3 tasks
RehanSD opened this issue Aug 28, 2023 · 2 comments
Open
2 of 3 tasks

BUG: int origin treated as nanoseconds since epoch time #54788

RehanSD opened this issue Aug 28, 2023 · 2 comments
Labels
Bug Datetime Datetime data dtype

Comments

@RehanSD
Copy link

RehanSD commented Aug 28, 2023

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

In [1]: import pandas as pd

In [2]: pd.to_datetime(0, origin=0)
Out[2]: Timestamp('1970-01-01 00:00:00')

In [3]: pd.to_datetime(0, origin=1)
Out[3]: Timestamp('1970-01-01 00:00:00.000000001')

In [4]: pd.__version__
Out[4]: '2.0.3'

In [5]: pd.to_datetime(0, origin=1)
Out[5]: Timestamp('1970-01-01 00:00:00.000000001')

In [6]: pd.to_datetime(1, unit="ns")
Out[6]: Timestamp('1970-01-01 00:00:00.000000001')

In [7]: pd.to_datetime(1, unit="ns") == pd.to_datetime(0, origin=1)
Out[7]: True

In [8]: pd.to_datetime(1, unit="ms") == pd.to_datetime(0, origin=1)
Out[8]: False

Issue Description

The documentation for pandas v2.0.3 state that an int or float origin will be treated as milliseconds and added to the epoch time, but seems like its being treated as nanoseconds? Not sure if this is a bug, or a typo in the documentation!

Expected Behavior

The resulting timestamp for pd.to_datetime(0, origin=1) should be equivalent to pd.to_datetime(1, unit="ms").

Installed Versions

INSTALLED VERSIONS

commit : 0f43794
python : 3.8.17.final.0
python-bits : 64
OS : Darwin
OS-release : 21.3.0
Version : Darwin Kernel Version 21.3.0: Wed Jan 5 21:37:58 PST 2022; root:xnu-8019.80.24~20/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.0.3
numpy : 1.24.4
pytz : 2023.3
dateutil : 2.8.2
setuptools : 68.0.0
pip : 23.2.1
Cython : None
pytest : 7.4.0
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.3
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 8.12.2
pandas_datareader: None
bs4 : 4.12.2
bottleneck : None
brotli : None
fastparquet : None
fsspec : 2023.6.0
gcsfs : None
matplotlib : 3.7.2
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 10.0.1
pyreadstat : None
pyxlsb : None
s3fs : 0.4.2
scipy : 1.10.1
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

@RehanSD RehanSD added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 28, 2023
@RehanSD
Copy link
Author

RehanSD commented Aug 29, 2023

Sort of related (but happy to open a second issue if preferred), but Timestamp-able origins cause rounding differences:

In [4]: origin = datetime.datetime(2023, 1, 1)
...
In [14]: pandas.to_datetime(pandas_df["float_col"], unit=unit)
Out[14]:
0   1970-01-01 00:00:00.000000091
1   1970-01-01 00:00:00.000000083
2   1970-01-01 00:00:00.000000025
3   1970-01-01 00:00:00.000000029
4   1970-01-01 00:00:00.000000018
5   1970-01-01 00:00:00.000000023
6   1970-01-01 00:00:00.000000034
7   1970-01-01 00:00:00.000000059
8   1970-01-01 00:00:00.000000036
9   1970-01-01 00:00:00.000000078
Name: float_col, dtype: datetime64[ns]

In [15]: pandas.to_datetime(pandas_df["float_col"], unit=unit, origin=origin)
Out[15]:
0   2023-01-01
1   2023-01-01
2   2023-01-01
3   2023-01-01
4   2023-01-01
5   2023-01-01
6   2023-01-01
7   2023-01-01
8   2023-01-01
9   2023-01-01
Name: float_col, dtype: datetime64[ns]

also happens with Timestamp objects:

In [22]: origin = pandas.Timestamp("01/01/2023")

In [23]: pandas.to_datetime(pandas_df["float_col"], unit=unit, origin=origin)
Out[23]:
0   2023-01-01
1   2023-01-01
2   2023-01-01
3   2023-01-01
4   2023-01-01
5   2023-01-01
6   2023-01-01
7   2023-01-01
8   2023-01-01
9   2023-01-01
Name: float_col, dtype: datetime64[ns]

In [24]: pandas.to_datetime(pandas_df["float_col"], unit=unit)
Out[24]:
0   1970-01-01 00:00:00.000000091
1   1970-01-01 00:00:00.000000083
2   1970-01-01 00:00:00.000000025
3   1970-01-01 00:00:00.000000029
4   1970-01-01 00:00:00.000000018
5   1970-01-01 00:00:00.000000023
6   1970-01-01 00:00:00.000000034
7   1970-01-01 00:00:00.000000059
8   1970-01-01 00:00:00.000000036
9   1970-01-01 00:00:00.000000078
Name: float_col, dtype: datetime64[ns]

In [25]: origin = pandas.Timestamp("01/01/2023 00:00:00.000000000")

In [26]: pandas.to_datetime(pandas_df["float_col"], unit=unit, origin=origin)
Out[26]:
0   2023-01-01
1   2023-01-01
2   2023-01-01
3   2023-01-01
4   2023-01-01
5   2023-01-01
6   2023-01-01
7   2023-01-01
8   2023-01-01
9   2023-01-01
Name: float_col, dtype: datetime64[ns]

In [27]: pandas.to_datetime(pandas_df["float_col"], unit=unit)
Out[27]:
0   1970-01-01 00:00:00.000000091
1   1970-01-01 00:00:00.000000083
2   1970-01-01 00:00:00.000000025
3   1970-01-01 00:00:00.000000029
4   1970-01-01 00:00:00.000000018
5   1970-01-01 00:00:00.000000023
6   1970-01-01 00:00:00.000000034
7   1970-01-01 00:00:00.000000059
8   1970-01-01 00:00:00.000000036
9   1970-01-01 00:00:00.000000078
Name: float_col, dtype: datetime64[ns]

@jbrockmendel jbrockmendel added the Datetime Datetime data dtype label Nov 1, 2023
@mroeschke mroeschke removed the Needs Triage Issue that has not been reviewed by a pandas team member label Jul 17, 2024
@kasaudhanshivani
Copy link

Hi! I’m Shivani Kasaudhan, a beginner in open source. I’d love to work on this issue as my first contribution to Pandas. Could you please assign it to me? 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Datetime Datetime data dtype
Projects
None yet
Development

No branches or pull requests

4 participants