在 Pandas 数据列中访问 total_seconds()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36156582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:55:25  来源:igfitidea点击:

Accessing total_seconds() in pandas data column

pythondatetimepandas

提问by user2994013

I want to create a new column in a pandas data frame that is the elapsed time from the start of the data frame. I am importing a log file into a data frame which has datatime info, but accessing the total_seconds()function in s_df['delta_t']is not working. It works if I access the individual elements of the column (s_df['delta_t'].iloc[8].total_seconds()), but I want to create a new column with total_seconds() and my attempts are failing.

我想在 Pandas 数据框中创建一个新列,它是从数据框开始经过的时间。我正在将日志文件导入具有数据时间信息的数据框中,但无法访问该total_seconds()函数s_df['delta_t']。如果我访问列 ( s_df['delta_t'].iloc[8].total_seconds())的各个元素,它会起作用,但是我想使用 total_seconds() 创建一个新列并且我的尝试失败了。

s_df['t'] = s_df.index  # s_df['t] is a column of datetime
s_df['delta_t'] = ( s_df['t'] - s_df['t'].iloc[0]) # time since start of data frame
s_df['elapsed_seconds'] = # want column s_df['delta_t'].total_seconds()

回答by EdChum

use .dtaccessor:

使用.dt访问器:

s_df['elapsed_seconds'] = s_df['delta_t'].dt.total_seconds()

Example:

例子:

In [82]:
df = pd.DataFrame({'date': pd.date_range(dt.datetime(2010,1,1), dt.datetime(2010,2,1))})
df['delta'] = df['date'] - df['date'].iloc[0]
df

Out[82]:
         date   delta
0  2010-01-01  0 days
1  2010-01-02  1 days
2  2010-01-03  2 days
3  2010-01-04  3 days
4  2010-01-05  4 days
5  2010-01-06  5 days
6  2010-01-07  6 days
7  2010-01-08  7 days
8  2010-01-09  8 days
9  2010-01-10  9 days
10 2010-01-11 10 days
11 2010-01-12 11 days
12 2010-01-13 12 days
13 2010-01-14 13 days
14 2010-01-15 14 days
15 2010-01-16 15 days
16 2010-01-17 16 days
17 2010-01-18 17 days
18 2010-01-19 18 days
19 2010-01-20 19 days
20 2010-01-21 20 days
21 2010-01-22 21 days
22 2010-01-23 22 days
23 2010-01-24 23 days
24 2010-01-25 24 days
25 2010-01-26 25 days
26 2010-01-27 26 days
27 2010-01-28 27 days
28 2010-01-29 28 days
29 2010-01-30 29 days
30 2010-01-31 30 days
31 2010-02-01 31 days

In [83]:
df['total_seconds'] = df['delta'].dt.total_seconds()
df

Out[83]:
         date   delta  total_seconds
0  2010-01-01  0 days              0
1  2010-01-02  1 days          86400
2  2010-01-03  2 days         172800
3  2010-01-04  3 days         259200
4  2010-01-05  4 days         345600
5  2010-01-06  5 days         432000
6  2010-01-07  6 days         518400
7  2010-01-08  7 days         604800
8  2010-01-09  8 days         691200
9  2010-01-10  9 days         777600
10 2010-01-11 10 days         864000
11 2010-01-12 11 days         950400
12 2010-01-13 12 days        1036800
13 2010-01-14 13 days        1123200
14 2010-01-15 14 days        1209600
15 2010-01-16 15 days        1296000
16 2010-01-17 16 days        1382400
17 2010-01-18 17 days        1468800
18 2010-01-19 18 days        1555200
19 2010-01-20 19 days        1641600
20 2010-01-21 20 days        1728000
21 2010-01-22 21 days        1814400
22 2010-01-23 22 days        1900800
23 2010-01-24 23 days        1987200
24 2010-01-25 24 days        2073600
25 2010-01-26 25 days        2160000
26 2010-01-27 26 days        2246400
27 2010-01-28 27 days        2332800
28 2010-01-29 28 days        2419200
29 2010-01-30 29 days        2505600
30 2010-01-31 30 days        2592000
31 2010-02-01 31 days        2678400