python - Cast types on the fly while creating dataframe -
i have kind of partial json object, dict:
{ u'20140816 00': {u'var': u'40.78'}, u'20140816 01': {u'var': u'53.24'}, u'20140816 02': {u'var': u'50.23'}, ... }
and want put in pandas dataframe.
however, change keys string datetime (datetime.strptime(key, '%y%m%d %h')
, cast var
value string float.
is possible while creating pandas dataframe, or if not easiest way recast types json object pandas dataframe.
you can use convert_objects
method convert strings desired dtypes.
normally read_
methods have date handling param , these can handle formats.
in case, seeing convert_objects
doesn't quite want can call pandas method to_datetime
convert column so:
df['time_col'] = pd.to_datetime(df['time_col'])
Comments
Post a Comment