I am been struggling to understand this date format in pandas. Basically I'd like to remove hour:min:second so I can only keep year-month-day format.
here is the output:
df.index
atetimeIndex(['2021-07-01', '2021-07-02', '2021-07-03', '2021-07-04',
'2021-07-05', '2021-07-06', '2021-07-07', '2021-07-08',
'2021-07-09', '2021-07-10',
...
'2021-12-23', '2021-12-24', '2021-12-25', '2021-12-26',
'2021-12-27', '2021-12-28', '2021-12-29', '2021-12-30',
'2021-12-31', '2022-01-01'],
dtype='datetime64[ns]', name='Date', length=185, freq=None)
from the above, you can see date format is year-month-day.
now when I use for loop to loop over index, the individual date becomes year-month-day hour:minut:second
for index_date,date in enumerate(df.index):
print(date)
2021-07-01 00:00:00
2021-07-02 00:00:00
2021-07-03 00:00:00
2021-07-04 00:00:00
2021-07-05 00:00:00
2021-07-06 00:00:00
why the date format is not year-month-day? Thanks for your help
Here is the detailed screenshot
CodePudding user response:
When you use df.index, you use the string representation of the pd.DatetimeIndex class whereas you use print(date) you use the string representation of the pd.Timedelta class.
>>> type(df.index)
pandas.core.indexes.datetimes.DatetimeIndex
>>> type(df.index[0]) # same as date in your loop
pandas._libs.tslibs.timestamps.Timestamp
Possible workaround for your loop:
# HERE ---v
for index_date, date in enumerate(df.index.date):
print(date)
# Output
2021-07-01
2021-07-02
2021-07-03
2021-07-04
2021-07-05
2021-07-06
...

