pandas icon indicating copy to clipboard operation
pandas copied to clipboard

BUG: the `seconds` attribute of `.dt.components` for pyarrow timestamp datatype seem faulty

Open MatusGasparik opened this issue 1 year ago • 1 comments

Pandas version checks

  • [X] I have checked that this issue has not already been reported.

  • [X] I have confirmed this bug exists on the latest version of pandas.

  • [X] I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd

assert pd.__version__ == '2.2.0'

# example from the book "Effective Pandas 2 (by Matt Harrison)"
classes = ['cs106', 'cs150', 'hist205', 'hist206', 'hist207']

start_dates = (
    pd.Series([
        '2015-03-08',
        '2015-03-08',
        '2015-03-09',
        '2015-03-09',
        '2015-03-11'
    ], dtype='datetime64[ns]', index=classes
).astype('timestamp[ns][pyarrow]'))

end_dates = (
    pd.Series([
        '2015-05-28 23:59:59', 
        '2015-06-01 3:00:00', 
        '2015-06-03', 
        '2015-06-02 14:20', 
        '2015-06-01'
    ], dtype='datetime64[ns]', index=classes
).astype('timestamp[ns][pyarrow]'))

duration = (end_dates - start_dates)

Issue Description

These are the data:

print(duration)
cs106      81 days 23:59:59
cs150      85 days 03:00:00
hist205    86 days 00:00:00
hist206    85 days 14:20:00
hist207    82 days 00:00:00
dtype: duration[ns][pyarrow]

I noticed that calling .dt.components on the above duration object behaves differently for the 'timestamp[ns][pyarrow]' vs. the 'timedelta64[ns]' data types. For the former, the seconds attribute (column) give a very high value (and I assume it is total seconds for the 'HH:MM:SS' part.

print(duration.dt.components)

results in:

   days  hours  minutes  seconds  milliseconds  microseconds  nanoseconds
0    81     23       59    86399             0             0            0
1    85      3        0    10800             0             0            0
2    86      0        0        0             0             0            0
3    85     14       20    51600             0             0            0
4    82      0        0        0             0             0            0

Expected Behavior

Converting to pandas 1.x 'timedelta64[ns]' give an expected result:

print(duration.astype('timedelta64[ns]').dt.components)

results in:

         days  hours  minutes  seconds  milliseconds  microseconds  nanoseconds
cs106      81     23       59       59             0             0            0
cs150      85      3        0        0             0             0            0
hist205    86      0        0        0             0             0            0
hist206    85     14       20        0             0             0            0
hist207    82      0        0        0             0             0            0

Installed Versions

INSTALLED VERSIONS

commit : f538741432edf55c6b9fb5d0d496d2dd1d7c2457 python : 3.12.1.final.0 python-bits : 64 OS : Darwin OS-release : 21.6.0 Version : Darwin Kernel Version 21.6.0: Sun Nov 6 23:31:13 PST 2022; root:xnu-8020.240.14~1/RELEASE_ARM64_T6000 machine : arm64 processor : arm byteorder : little LC_ALL : None LANG : de_DE.UTF-8 LOCALE : de_DE.UTF-8

pandas : 2.2.0 numpy : 1.26.4 pytz : 2024.1 dateutil : 2.8.2 setuptools : 69.0.3 pip : 24.0 Cython : None pytest : None hypothesis : None sphinx : None blosc : None feather : None xlsxwriter : None lxml.etree : None html5lib : None pymysql : None psycopg2 : None jinja2 : None IPython : 8.21.0 pandas_datareader : None adbc-driver-postgresql: None adbc-driver-sqlite : None bs4 : None bottleneck : None dataframe-api-compat : None fastparquet : None fsspec : None gcsfs : None matplotlib : None numba : None numexpr : None odfpy : None openpyxl : None pandas_gbq : None pyarrow : 15.0.0 pyreadstat : None python-calamine : None pyxlsb : None s3fs : None scipy : None sqlalchemy : None tables : None tabulate : None xarray : None xlrd : None zstandard : None tzdata : 2023.4 qtpy : None pyqt5 : None

MatusGasparik avatar Feb 11 '24 10:02 MatusGasparik

Thanks for the report. Confirmed on main, further investigations and PRs to fix are welcome!

rhshadrach avatar Feb 15 '24 04:02 rhshadrach

Will try to work on this.

St0rmie avatar Feb 26 '24 14:02 St0rmie

take

St0rmie avatar Feb 27 '24 13:02 St0rmie