Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
421 views
in Technique[技术] by (71.8m points)

python - Python:如何在pandas列中将时间戳转换为日期?(Python: how to convert timestamp to date in a pandas column?)

I have a dataframe like the following

(我有一个如下的数据框)

print (df)
            T
0  1573119509
1  1573119512
2  1573119517
3  1573119520

I would like to convert the column T to a date.

(我想将T列转换为日期。)

This is what I am doing:

(这就是我在做什么:)

df['Td'] = pd.to_datetime(df['T']).apply(lambda x: x.date())

but it returns:

(但它返回:)

print (df)
            T           Td
0  1573119509   1970-01-01
1  1573119512   1970-01-01
2  1573119517   1970-01-01
3  1573119520   1970-01-01
  ask by emax translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use parameter unit='s' in to_datetime and then Series.dt.date :

(在to_datetime使用参数unit='s' ,然后在Series.dt.date :)

df['Td'] = pd.to_datetime(df['T'], unit='s').dt.date
print (df)
            T          Td
0  1573119509  2019-11-07
1  1573119512  2019-11-07
2  1573119517  2019-11-07
3  1573119520  2019-11-07

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...