Skip to content

BUG: DateTimeIndex + DateTimeIndex.freq giving incorrect result if timezone is set. #39398

Closed
@rwijtvliet

Description

@rwijtvliet
  • 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
i = pd.date_range('2020-03-27', freq='D', periods=4, tz='Europe/Berlin')
i2 = i + i.freq
i2.freq = 'D' # ValueError: Inferred frequency None from passed values does not conform to passed frequency D

Problem description

i is as expected.

DatetimeIndex(['2020-03-27 00:00:00+01:00', '2020-03-28 00:00:00+01:00',
               '2020-03-29 00:00:00+01:00', '2020-03-30 00:00:00+02:00'],
              dtype='datetime64[ns, Europe/Berlin]', freq='D') #good

but i2 has two issues:

DatetimeIndex(['2020-03-29 00:00:00+01:00', '2020-03-30 01:00:00+02:00',
               '2020-03-31 00:00:00+02:00', '2020-04-01 00:00:00+02:00'],
              dtype='datetime64[ns, Europe/Berlin]', freq='D') 
#issue: 2020-03-30 timestamp not at midnight
#issue: i2 has attribute `freq='D'`, but pd.infer_freq returns None

Expected Output

i2 to be

DatetimeIndex(['2020-03-29 00:00:00+01:00', '2020-03-30 00:00:00+02:00',
               '2020-03-31 00:00:00+02:00', '2020-04-01 00:00:00+02:00'],
              dtype='datetime64[ns, Europe/Berlin]', freq='D') 

(2020-03-30 00:00:00+02:00 is only change),
with i2.freq not returning None

Related

May be related to #38243

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 3e89b4c
python : 3.8.5.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.18362
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 10, GenuineIntel
byteorder : little
LC_ALL : None
LANG : en
LOCALE : de_DE.cp1252

pandas : 1.2.0
numpy : 1.19.5
pytz : 2020.5
dateutil : 2.8.1
pip : 20.3.3
setuptools : 51.1.2.post20210112
Cython : None
pytest : 6.2.1
hypothesis : None
sphinx : 3.4.3
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.12.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.3.3
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.6.0
sqlalchemy : 1.3.22
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions