Python - 将datetime列转换为秒[重复]

时间:2021-12-07 11:28:35

This question already has an answer here:

这个问题在这里已有答案:

spent a long time trying to find this out but getting stuck.

花了很长时间试图找到这个,但卡住了。

I have a date column (called 'Time') which contains days/hours/mins etc (timedelta). I have created a new column in my dataframe and I want to convert the 'Time' column into seconds and put it in the new column for each row.

我有一个日期列(称为“时间”),其中包含天/小时/分钟等(timedelta)。我在数据框中创建了一个新列,我希望将“时间”列转换为秒,并将其放入每行的新列中。

Does anyone have any pointers? All I can find on the internet is how to convert your column, not create a new column and convert another one.

有没有人有任何指针?我在互联网上找到的只是如何转换列,而不是创建新列并转换另一列。

Thank you in advance!

先谢谢你!

2 个解决方案

#1


11  

I think you need total_seconds:

我认为你需要total_seconds:

print (df['col'].dt.total_seconds())

Sample:

df = pd.DataFrame({'date1':pd.date_range('2015-01-01', periods=3),
                   'date2':pd.date_range('2015-01-01 02:00:00', periods=3, freq='23H')})

print (df)
       date1               date2
0 2015-01-01 2015-01-01 02:00:00
1 2015-01-02 2015-01-02 01:00:00
2 2015-01-03 2015-01-03 00:00:00

df['diff'] = df['date2'] - df['date1']
df['seconds'] = df['diff'].dt.total_seconds()

print (df)
       date1               date2     diff  seconds
0 2015-01-01 2015-01-01 02:00:00 02:00:00   7200.0
1 2015-01-02 2015-01-02 01:00:00 01:00:00   3600.0
2 2015-01-03 2015-01-03 00:00:00 00:00:00      0.0

df['diff'] = df['date2'] - df['date1']
df['diff'] = df['diff'].dt.total_seconds()

print (df)
       date1               date2    diff
0 2015-01-01 2015-01-01 02:00:00  7200.0
1 2015-01-02 2015-01-02 01:00:00  3600.0
2 2015-01-03 2015-01-03 00:00:00     0.0

If need cast to int:

如果需要强制转换为int:

df['diff'] = df['date2'] - df['date1']
df['diff'] = df['diff'].dt.total_seconds().astype(int)

print (df)
       date1               date2  diff
0 2015-01-01 2015-01-01 02:00:00  7200
1 2015-01-02 2015-01-02 01:00:00  3600
2 2015-01-03 2015-01-03 00:00:00     0

#2


3  

Let's assume your DataFrame's name is df.

我们假设你的DataFrame的名字是df。

If you want to create a new column with the seconds you should do the following:

如果要使用秒创建新列,则应执行以下操作:

df['newColumn'] = df['Time'].dt.total_seconds()

#1


11  

I think you need total_seconds:

我认为你需要total_seconds:

print (df['col'].dt.total_seconds())

Sample:

df = pd.DataFrame({'date1':pd.date_range('2015-01-01', periods=3),
                   'date2':pd.date_range('2015-01-01 02:00:00', periods=3, freq='23H')})

print (df)
       date1               date2
0 2015-01-01 2015-01-01 02:00:00
1 2015-01-02 2015-01-02 01:00:00
2 2015-01-03 2015-01-03 00:00:00

df['diff'] = df['date2'] - df['date1']
df['seconds'] = df['diff'].dt.total_seconds()

print (df)
       date1               date2     diff  seconds
0 2015-01-01 2015-01-01 02:00:00 02:00:00   7200.0
1 2015-01-02 2015-01-02 01:00:00 01:00:00   3600.0
2 2015-01-03 2015-01-03 00:00:00 00:00:00      0.0

df['diff'] = df['date2'] - df['date1']
df['diff'] = df['diff'].dt.total_seconds()

print (df)
       date1               date2    diff
0 2015-01-01 2015-01-01 02:00:00  7200.0
1 2015-01-02 2015-01-02 01:00:00  3600.0
2 2015-01-03 2015-01-03 00:00:00     0.0

If need cast to int:

如果需要强制转换为int:

df['diff'] = df['date2'] - df['date1']
df['diff'] = df['diff'].dt.total_seconds().astype(int)

print (df)
       date1               date2  diff
0 2015-01-01 2015-01-01 02:00:00  7200
1 2015-01-02 2015-01-02 01:00:00  3600
2 2015-01-03 2015-01-03 00:00:00     0

#2


3  

Let's assume your DataFrame's name is df.

我们假设你的DataFrame的名字是df。

If you want to create a new column with the seconds you should do the following:

如果要使用秒创建新列,则应执行以下操作:

df['newColumn'] = df['Time'].dt.total_seconds()