Skip to content
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: Regression adding Timedelta to DatetimeIndex, get ValueError: Inferred frequency None from passed values does not conform to passed frequency #37295

Closed
3 tasks done
nathanielatom opened this issue Oct 21, 2020 · 7 comments · Fixed by #37780
Labels
Regression Functionality that used to work in a prior pandas version Timeseries
Milestone

Comments

@nathanielatom
Copy link

nathanielatom commented Oct 21, 2020

  • 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.


Code Sample, a copy-pastable example

import pandas as pd
pd.date_range(start=pd.Timestamp('2019-03-26 00:00:00-0400', tz='Canada/Eastern'), 
              end=pd.Timestamp('2020-10-17 00:00:00-0400', tz='Canada/Eastern'), 
              freq='D') + pd.Timedelta('1D')

Traceback

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~/.miniconda/lib/python3.7/site-packages/pandas/core/arrays/datetimelike.py in _validate_frequency(cls, index, freq, **kwargs)
   1156             if not np.array_equal(index.asi8, on_freq.asi8):
-> 1157                 raise ValueError
   1158         except ValueError as e:

ValueError: 

The above exception was the direct cause of the following exception:

ValueError                                Traceback (most recent call last)
<ipython-input-2-d8f454fa3ee3> in <module>
----> 1 pd.date_range(start=pd.Timestamp('2019-03-26 00:00:00-0400', tz='Canada/Eastern'), end=pd.Timestamp('2020-10-17 00:00:00-0400', tz='Canada/Eastern'), freq='D') + pd.Timedelta('1D')

~/.miniconda/lib/python3.7/site-packages/pandas/core/indexes/extension.py in method(self, other)
    145             return NotImplemented
    146         meth = getattr(self._data, opname)
--> 147         result = meth(_maybe_unwrap_index(other))
    148         return _wrap_arithmetic_op(self, other, result)
    149 

~/.miniconda/lib/python3.7/site-packages/pandas/core/ops/common.py in new_method(self, other)
     63         other = item_from_zerodim(other)
     64 
---> 65         return method(self, other)
     66 
     67     return new_method

~/.miniconda/lib/python3.7/site-packages/pandas/core/arrays/datetimelike.py in __add__(self, other)
   1399             result = self._add_nat()
   1400         elif isinstance(other, (Tick, timedelta, np.timedelta64)):
-> 1401             result = self._add_timedeltalike_scalar(other)
   1402         elif isinstance(other, BaseOffset):
   1403             # specifically _not_ a Tick

~/.miniconda/lib/python3.7/site-packages/pandas/core/arrays/datetimelike.py in _add_timedeltalike_scalar(self, other)
   1254             new_freq = self.freq
   1255 
-> 1256         return type(self)(new_values, dtype=self.dtype, freq=new_freq)
   1257 
   1258     def _add_timedelta_arraylike(self, other):

~/.miniconda/lib/python3.7/site-packages/pandas/core/arrays/datetimes.py in __init__(self, values, dtype, freq, copy)
    282 
    283         if inferred_freq is None and freq is not None:
--> 284             type(self)._validate_frequency(self, freq)
    285 
    286     @classmethod

~/.miniconda/lib/python3.7/site-packages/pandas/core/arrays/datetimelike.py in _validate_frequency(cls, index, freq, **kwargs)
   1169                 f"Inferred frequency {inferred} from passed values "
   1170                 f"does not conform to passed frequency {freq.freqstr}"
-> 1171             ) from e
   1172 
   1173     # monotonicity/uniqueness properties are called via frequencies.infer_freq,

ValueError: Inferred frequency None from passed values does not conform to passed frequency D

Problem description

Unexpected ValueError when adding a Timedelta to a timezone-aware DatetimeIndex. It appears using timezone-aware Timestamp objects is necessary to reproduce - simple date strings won't.

Expected Output

The output from the same code sample in v0.25.3 is:

DatetimeIndex(['2019-03-27 00:00:00-04:00', '2019-03-28 00:00:00-04:00',
               '2019-03-29 00:00:00-04:00', '2019-03-30 00:00:00-04:00',
               '2019-03-31 00:00:00-04:00', '2019-04-01 00:00:00-04:00',
               '2019-04-02 00:00:00-04:00', '2019-04-03 00:00:00-04:00',
               '2019-04-04 00:00:00-04:00', '2019-04-05 00:00:00-04:00',
               ...
               '2020-10-09 00:00:00-04:00', '2020-10-10 00:00:00-04:00',
               '2020-10-11 00:00:00-04:00', '2020-10-12 00:00:00-04:00',
               '2020-10-13 00:00:00-04:00', '2020-10-14 00:00:00-04:00',
               '2020-10-15 00:00:00-04:00', '2020-10-16 00:00:00-04:00',
               '2020-10-17 00:00:00-04:00', '2020-10-18 00:00:00-04:00'],
              dtype='datetime64[ns, Canada/Eastern]', length=572, freq=None)

don't know about later versions, before v1.1.3.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : db08276
python : 3.7.6.final.0
python-bits : 64
OS : Darwin
OS-release : 16.7.0
Version : Darwin Kernel Version 16.7.0: Thu Dec 20 21:53:35 PST 2018; root:xnu-3789.73.31~1/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_CA.UTF-8
LOCALE : en_CA.UTF-8

pandas : 1.1.3
numpy : 1.19.2
pytz : 2020.1
dateutil : 2.8.1
pip : 20.2.3
setuptools : 50.3.0
Cython : 0.29.20
pytest : 6.1.1
hypothesis : None
sphinx : 1.8.5
blosc : 1.9.2
feather : None
xlsxwriter : 1.2.1
lxml.etree : 4.5.2
html5lib : 0.9999999
pymysql : 0.9.3
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.16.1
pandas_datareader: 0.9.0
bs4 : 4.9.3
bottleneck : 1.3.2
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.2.1
numexpr : 2.7.1
odfpy : None
openpyxl : 2.6.3
pandas_gbq : None
pyarrow : None
pytables : None
pyxlsb : None
s3fs : None
scipy : 1.5.2
sqlalchemy : 1.3.11
tables : 3.6.1
tabulate : None
xarray : 0.16.1
xlrd : 1.2.0
xlwt : 1.3.0
numba : 0.51.2

@nathanielatom nathanielatom added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 21, 2020
@jreback
Copy link
Contributor

jreback commented Oct 21, 2020

try on master pls

@nathanielatom
Copy link
Author

Yep, same behaviour as v1.1.3 on master; edited.

@dsaxton
Copy link
Member

dsaxton commented Oct 21, 2020

Regression is due to #32799

cc @jbrockmendel

@dsaxton dsaxton added Regression Functionality that used to work in a prior pandas version Timeseries and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 21, 2020
@dsaxton dsaxton added this to the 1.1.4 milestone Oct 21, 2020
@jbrockmendel
Copy link
Member

jbrockmendel commented Oct 21, 2020

cc @mroeschke this looks like another case where we need freq="D" to mean DayDST or something

@jbrockmendel
Copy link
Member

OK, so there are at least three things wrong here:

  1. In this context, freq="D" needs to mean something like DayDST
  2. dti.infer_freq() is incorrectly returning "D" instead of None
  3. _add_timedeltalike_scalar should use type(self)._simple_new instead of type(self) (once the other two problems are resolved)

@simonjayhawkins simonjayhawkins modified the milestones: 1.1.4, 1.1.5 Oct 29, 2020
@simonjayhawkins
Copy link
Member

moved off 1.1.4 milestone (scheduled for release tomorrow) as no PRs to fix in the pipeline

@nathanielatom
Copy link
Author

Thanks for the heads up

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Regression Functionality that used to work in a prior pandas version Timeseries
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants