I am trying to do loops to convert a column of timestamp in a dataframe into ISO 8601 format. However, it keeps going back to the 1970 data with weird hour:minute:second. But if I just choose one timestamp and run the middle code "tz=... iso.format())", it works fine.
df= pd.read_csv('file.csv')
for i in range(len(df)):
timestamp=df.loc[i,'timestamp']
# print(timestamp)
#forloops to print ISO 8601 format
res=[]
def ISOformat():
tz = pytz.timezone('America/Los_Angeles')
print(datetime.fromtimestamp(i, tz).isoformat())
for i in range(len(df)):
res.append(ISOformat())
#make a dataframe of the ISO 8601
iso_8601_test= pd.DataFrame({'ISO_8601_time':res})
I like my output to be like this: 2011-12-31T16:05:00-08:00 not the 1970 time.
Example of my timestamp:
timestamp
1325376300
1325376600
1325376900
1325377200
1325377500
1325377800
1325378100
1325378400
1325378700
1325379000
1325379300
1325379600
1325379900
I think you can use the Timestamp
function to create the column the way you want. You can use apply
:
res = df['timestamp'].apply(lambda time: pd.Timestamp(time,
tz='America/Los_Angeles',unit='s').isoformat())
and then create the dataframe the way you did:
iso_8601_test= pd.DataFrame({'ISO_8601_time':res})
your output is like (with the first two number in your input data):
ISO_8601_time
0 2011-12-31T16:05:00-08:00
1 2011-12-31T16:10:00-08:00