datetime - Can't convert date time to python readable format -
i have range of date time follows:
date 2017-04-02 12:00 2017-04-02 01:00 2017-04-02 02:00 2017-04-02 03:00 2017-04-02 04:00 2017-04-02 05:00 2017-04-02 06:00 2017-04-02 07:00 2017-04-02 08:00 2017-04-02 09:00 2017-04-02 10:00 i want convert of them python date time plot in x-axis.i used following code:
dates=fire['date']#copy date value in dataframe x = dt.datetime.strptime('dates',"%y-%m-%d %i:%m %p").date() but gives me error as:
time data 'd' not match format '%y-%m-%d %i:%m %p' how can convert these dates python readable dates?
d variable, need remove quotes:
x = [dt.datetime.strptime(d,"%y-%m-%d %i:%m %p").date() d in dates] edit : reproducible example
following op request, here's reproducible example:
import pandas pd import datetime dt stringio import stringio data=""" date 2017-04-02 12:00 2017-04-02 01:00 2017-04-02 02:00 2017-04-02 03:00 2017-04-02 04:00 2017-04-02 05:00 2017-04-02 06:00 2017-04-02 07:00 2017-04-02 08:00 2017-04-02 09:00 2017-04-02 10:00 """ # stringio used simulate reading csv file df = pd.read_csv(stringio(data)) print(df.head()) dates = df['date'] x = [dt.datetime.strptime(d,"%y-%m-%d %i:%m %p").date() d in dates] print(x) the output:
[datetime.date(2017, 4, 2), datetime.date(2017, 4, 2), datetime.date(2017, 4, 2), datetime.date(2017, 4, 2), datetime.date(2017, 4, 2), datetime.date(2017, 4, 2), datetime.date(2017, 4, 2), datetime.date(2017, 4, 2), datetime.date(2017, 4, 2), datetime.date(2017, 4, 2), datetime.date(2017, 4, 2)] on side note, pandas can parse dates follows:
df2 = pd.read_csv(stringio(data), parse_dates=['date']) the output:
date 0 2017-04-02 00:00:00 1 2017-04-02 01:00:00 2 2017-04-02 02:00:00 3 2017-04-02 03:00:00 4 2017-04-02 04:00:00
Comments
Post a Comment