time in strings format: python -
i using function bring date consistent in column, goes this
from dateutil.parser import parse def dateconv1(x): c = parse(x) return c
so if use as, works fine
in[15]:dateconv1("1 / 22 / 2016 15:03") out[15]:datetime.datetime(2016, 1, 22, 15, 3)
but when pass in variable
a= 1 / 22 / 2016 15:03 in[16]:dateconv1(str(a))
it doesn't work, how bring in quotes or string, every appreciating
assuming you talking pandas dataset, can use pandas to_datetime() method:
in [66]: dates = ['1 / 22 / 2016 15:03', 'jul 22 2016 12:32pm', 'jul 22 2016 5:40pm', ....: 'jul 22 2016 8:31pm', 'jul 23 2016 2:01pm', 'jul 23 2016 7:24pm', ....: 'jul 24 2016 4:30pm', 'aug 1 2016 4:00pm', 'aug 1 2016 7:49pm'] in [67]: df = pd.dataframe({'d':dates}) in [68]: df.dtypes out[68]: d object dtype: object
d object
- means d
column of string (object) dtype
in [69]: df out[69]: d 0 1 / 22 / 2016 15:03 1 jul 22 2016 12:32pm 2 jul 22 2016 5:40pm 3 jul 22 2016 8:31pm 4 jul 23 2016 2:01pm 5 jul 23 2016 7:24pm 6 jul 24 2016 4:30pm 7 aug 1 2016 4:00pm 8 aug 1 2016 7:49pm
let's convert datetime
dtype:
in [70]: df.d = pd.to_datetime(df.d) in [71]: df out[71]: d 0 2016-01-22 15:03:00 1 2016-07-22 12:32:00 2 2016-07-22 17:40:00 3 2016-07-22 20:31:00 4 2016-07-23 14:01:00 5 2016-07-23 19:24:00 6 2016-07-24 16:30:00 7 2016-08-01 16:00:00 8 2016-08-01 19:49:00
check dtype again:
in [72]: df.dtypes out[72]: d datetime64[ns] dtype: object
Comments
Post a Comment