I have a pandas dataframe with a column iso
in datetime64[ns]
format. My goal is to first convert this column to the ISO 8601 format, and then to a plain string.
dtrain.shape
(10886, 12)
dtrain.dtypes
season int64
holiday int64
workingday int64
weather int64
temp float64
atemp float64
humidity int64
windspeed float64
casual int64
registered int64
count int64
iso datetime64[ns]
I have tried a few methods mentioned on StackOverflow, which throw errors:
dtrain['iso'].astype(datetime)
dtrain['iso'].tolist()/1e9
The following works, but converts the column to a Python object which contains both formats and cannot be converted to string with str()
or as.type('str')
dtrain['iso'] =dtrain['iso'].dt.strftime('%Y-%m-%dT%H:%M%:%SZ')
dtrain['iso'].dtype
dtype('O')
dtrain['iso'] = dtrain['iso'].astype('str')
dtrain['iso']
datetime
2011-01-01 00:00:00 2011-01-01T00:00:00Z
2011-01-01 01:00:00 2011-01-01T01:00:00Z
2011-01-01 02:00:00 2011-01-01T02:00:00Z
2011-01-01 03:00:00 2011-01-01T03:00:00Z
2011-01-01 04:00:00 2011-01-01T04:00:00Z
...
2012-12-19 19:00:00 2012-12-19T19:00:00Z
2012-12-19 20:00:00 2012-12-19T20:00:00Z
2012-12-19 21:00:00 2012-12-19T21:00:00Z
2012-12-19 22:00:00 2012-12-19T22:00:00Z
2012-12-19 23:00:00 2012-12-19T23:00:00Z
Name: iso, Length: 10886, dtype: object
What is the workaround to convert a pandas dataframe object column to a string column (containing only the ISO 8601 format) in this case?