I have an issue that is plaguing me. Importing some data from .csv into Python gives a vector of dates in the format:
EDIT: Actually, it's not a vector of dates. Data is imported into a dataframe, so what I have to work on is a DatetimeIndex
object.
1961-01-01 09:00:00
I want to drop the hours part and keep only the YYYY-MM-DD part, but I can not find a way to do it. Pandas to_datetime
does not do what I need:
pd.to_datetime("1961-01-01 09:00:00", format=('%Y-%m-%d'))
Timestamp('1961-01-01 09:00:00')
While datetime.strptime would not just drop the hour:
from datetime import datetime as dt
dt.strptime("1961-01-01 09:00:00", "%Y-%m-%d")
ValueError: unconverted data remains: 09:00:00
Any suggestions?
EDIT 2: Thanks to @WeizhongTu, I found a solution. Operating on the dataframe with DatetimeIndex
index we can use:
mydf.index = mydf.index.values.astype('M8[D]')
That gives:
mydf.index[0:10]
DatetimeIndex(['1961-01-01', '1961-01-02', '1961-01-03', '1961-01-04',
'1961-01-05', '1961-01-06', '1961-01-07', '1961-01-08',
'1961-01-09', '1961-01-10'],
dtype='datetime64[ns]', freq=None)