Skip to content

BUG: Inconsistent initialization of timedelta64 between Series vs numpy & pd.TimeDelta #35465

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

Closed
2 of 3 tasks
galipremsagar opened this issue Jul 29, 2020 · 4 comments · Fixed by #47801
Closed
2 of 3 tasks
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions Timedelta Timedelta data type

Comments

@galipremsagar
Copy link

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

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

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

The creation of a pandas Series with any dtype other than timedelta64[ns] is discarding values and setting the entire series to 0's. Whereas numpy.array and pd.TimeDelta are correctly instantiating and preserving the passed values which can be retrieved by a type-cast too.

Code Sample, a copy-pastable example

>>> import pandas as pd
>>> pd.__version__
'1.1.0'
>>> pd.Series([1000000, 200000, 3000000], dtype='timedelta64[ns]')
0   0 days 00:00:00.001000
1   0 days 00:00:00.000200
2   0 days 00:00:00.003000
dtype: timedelta64[ns]
>>> pd.Series([1000000, 200000, 3000000], dtype='timedelta64[s]')
0   0 days
1   0 days
2   0 days
dtype: timedelta64[ns]
>>> pd.Series([1000000, 200000, 3000000], dtype='timedelta64[s]').astype('int')
0    0
1    0
2    0
dtype: int64
>>> pd.Series([1000000, 200000, 3000000], dtype='timedelta64[ns]').astype('int')
0    1000000
1     200000
2    3000000
dtype: int64

>>> import numpy as np
>>> np.array([1000000, 200000, 3000000], dtype='timedelta64[ns]')
array([1000000,  200000, 3000000], dtype='timedelta64[ns]')
>>> np.array([1000000, 200000, 3000000], dtype='timedelta64[s]')
array([1000000,  200000, 3000000], dtype='timedelta64[s]')
>>> np.array([1000000, 200000, 3000000], dtype='timedelta64[s]').astype('int')
array([1000000,  200000, 3000000])

>>> pd.Timedelta(10)
Timedelta('0 days 00:00:00.000000010')
>>> pd.Timedelta(10, unit='s')
Timedelta('0 days 00:00:10')
>>> pd.Timedelta(10, unit='ns')
Timedelta('0 days 00:00:00.000000010')

Problem description

The expected behaviour is Series initialization must be in-line with numpy array initialization in this case.

Expected Output

Output of pd.show_versions()

INSTALLED VERSIONS

commit : d9fff27
python : 3.7.3.final.0
python-bits : 64
OS : Darwin
OS-release : 19.6.0
Version : Darwin Kernel Version 19.6.0: Sun Jul 5 00:43:10 PDT 2020; root:xnu-6153.141.1~9/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : en_US.UTF-8
pandas : 1.1.0
numpy : 1.19.0
pytz : 2020.1
dateutil : 2.8.1
pip : 20.1.1
setuptools : 49.1.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 : 2.11.2
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@galipremsagar galipremsagar added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 29, 2020
@simonjayhawkins
Copy link
Member

Thanks @galipremsagar for the report.

Further investigation and PRs welcome.

@simonjayhawkins simonjayhawkins added this to the Contributions Welcome milestone Jul 30, 2020
@simonjayhawkins simonjayhawkins added Constructors Series/DataFrame/Index/pd.array Constructors Timedelta Timedelta data type and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 30, 2020
@simonjayhawkins
Copy link
Member

I'll mark as a regression as this raised Exception in 0.25.3

>>> pd.__version__
'0.25.3'
>>>
>>> pd.Series([1000000, 200000, 3000000], dtype="timedelta64[s]")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\simon\Anaconda3\lib\site-packages\pandas\core\series.py", line 314, in __init__
    data = sanitize_array(data, index, dtype, copy, raise_cast_failure=True)
  File "C:\Users\simon\Anaconda3\lib\site-packages\pandas\core\internals\construction.py", line 679, in sanitize_array
    subarr = _try_cast(data, dtype, copy, raise_cast_failure)
  File "C:\Users\simon\Anaconda3\lib\site-packages\pandas\core\internals\construction.py", line 784, in _try_cast
    subarr = maybe_cast_to_datetime(arr, dtype)
  File "C:\Users\simon\Anaconda3\lib\site-packages\pandas\core\dtypes\cast.py", line 1035, in maybe_cast_to_datetime
    "dtype [{dtype}]".format(dtype=dtype)
TypeError: cannot convert timedeltalike to dtype [timedelta64[s]]
>>>

@simonjayhawkins simonjayhawkins added the Regression Functionality that used to work in a prior pandas version label Jul 30, 2020
@galipremsagar
Copy link
Author

So is the expected behavior to raise an exception or initialization similar to numpy?

@mroeschke
Copy link
Member

I think the recent non-nano support probably fixed the Series initialization case with timedelta64[s] case. Could use a unit test

In [1]: >>> pd.Series([1000000, 200000, 3000000], dtype='timedelta64[ns]')
Out[1]:
0   0 days 00:00:00.001000
1   0 days 00:00:00.000200
2   0 days 00:00:00.003000
dtype: timedelta64[ns]

In [2]: >>> pd.Series([1000000, 200000, 3000000], dtype='timedelta64[s]')
Out[2]:
0   0 days 00:00:00.001000
1   0 days 00:00:00.000200
2   0 days 00:00:00.003000
dtype: timedelta64[ns]

In [3]: >>> pd.Series([1000000, 200000, 3000000], dtype='timedelta64[s]').astype('int')
Out[3]:
0    1000000
1     200000
2    3000000
dtype: int64

In [4]: >>> pd.Series([1000000, 200000, 3000000], dtype='timedelta64[ns]').astype('int')
Out[4]:
0    1000000
1     200000
2    3000000
dtype: int64

@mroeschke mroeschke added good first issue Needs Tests Unit test(s) needed to prevent regressions and removed Bug Regression Functionality that used to work in a prior pandas version Constructors Series/DataFrame/Index/pd.array Constructors labels Jul 6, 2022
mroeschke pushed a commit that referenced this issue Jul 27, 2022
* TST GH35465
Tests for repr of Series with dtype timedelta64

* pre commit test file

* TST GH35465
Tests for Series constructs  with dtype timedelta64

* restore pandas/tests/series/test_repr.py

* perform pre-commit

* use int64 for tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions Timedelta Timedelta data type
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants