Skip to content

BUG: date_range and DatetimeIndex return different formats with a single timestamp for midnight #53470

Closed
@pmlpm1986

Description

@pmlpm1986

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

import pandas as pd
# DatetimeIndex returns different formats with the same qualitative input when only one timestamp is requested
first_index = pd.DatetimeIndex(["2012-01-01 00:00:00"],
                               freq=str(60)+'T')
print(first_index)
second_index = pd.DatetimeIndex(["2012-01-01 01:00:00"],
                                freq=str(60)+'T')
print(second_index)
# date_range returns different formats with the same qualitative input when only one timestamp is requested
third_index = pd.date_range("2012-01-01 00:00:00",
                            periods=1,
                            freq=str(60)+'T')
print(third_index)
fourth_index = pd.date_range("2012-01-01 01:00:00",
                             periods=1,
                             freq=str(60)+'T')
print(fourth_index)
# the problem disappears when there are multiple samples
fifth_index = pd.DatetimeIndex(["2012-01-01 00:00:00","2012-01-01 01:00:00"],
                               freq=str(60)+'T')
print(fifth_index)
sixth_index = pd.date_range("2012-01-01 00:00:00",
                            periods=2,
                            freq=str(60)+'T')
print(sixth_index)

Issue Description

I have been trying to create DatetimeIndexes whose number of entries is determined by a parameter.

In doing this, I noticed that the date_range and DatetimeIndex return different formats for the same qualitative input (in the code above, a datetime-formatted string) and I do not know how to adjust the output format through the parameters. This happens when only one timestamp is provided/requested and it is for midnight.

Expected Behavior

By providing the same qualitative input (a datetime-formatted string), I was expecting the DatetimeIndex constructor and the date_range method to return the same format. This makes comparisons between time-indexes more cumbersome.

Installed Versions

INSTALLED VERSIONS

commit : 965ceca
python : 3.11.2.final.0
python-bits : 64
OS : Linux
OS-release : 6.1.0-7-amd64
Version : #1 SMP PREEMPT_DYNAMIC Debian 6.1.20-2 (2023-04-08)
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8

pandas : 2.0.2
numpy : 1.24.3
pytz : 2023.3
dateutil : 2.8.2
setuptools : 66.1.1
pip : 23.0.1
Cython : None
pytest : 7.3.1
hypothesis : None
sphinx : 7.0.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.13.1
pandas_datareader: None
bs4 : 4.12.2
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.7.1
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.10.1
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : 2.3.1
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions