将Timedelta转换为秒 - 错误

时间:2016-11-11 12:01:02

标签: pandas timedelta

我有一个df看起来像:

{'avg_time_diff': {22836: Timedelta('0 days 00:02:04.810879'),
  23533: Timedelta('0 days 00:03:35.242784'),
  45297: Timedelta('0 days 00:04:02.764130'),
  56232: Timedelta('0 days 00:02:48.427043'),
  92306: Timedelta('0 days 00:00:18.396753')},
 'count': {22836: 1671, 23533: 879, 45297: 859, 56232: 1130, 92306: 7803},
 'ipAddress': {22836: u'178.73.210.51',
  23533: u'185.125.168.216',
  45297: u'45.32.241.181',
  56232: u'77.66.48.87',
  92306: u'90.180.104.16'}}

我想将avg_time_diff列转换为数字量,以秒为单位。

我尝试了不同的方式,即:

df_diff['avg_time_diff'].dt.seconds

但我明白了:

AttributeError: Can only use .dt accessor with datetimelike values

这是什么意思?

1 个答案:

答案 0 :(得分:1)

对我而言,它完美无缺:

d = {'avg_time_diff': {22836: pd.Timedelta('0 days 00:02:04.810879'),
  23533: pd.Timedelta('0 days 00:03:35.242784'),
  45297: pd.Timedelta('0 days 00:04:02.764130'),
  56232: pd.Timedelta('0 days 00:02:48.427043'),
  92306: pd.Timedelta('0 days 00:00:18.396753')},
 'count': {22836: 1671, 23533: 879, 45297: 859, 56232: 1130, 92306: 7803},
 'ipAddress': {22836: u'178.73.210.51',
  23533: u'185.125.168.216',
  45297: u'45.32.241.181',
  56232: u'77.66.48.87',
  92306: u'90.180.104.16'}}

df_diff = pd.DataFrame(d)  
print (df_diff)
        avg_time_diff  count        ipAddress
22836 00:02:04.810879   1671    178.73.210.51
23533 00:03:35.242784    879  185.125.168.216
45297 00:04:02.764130    859    45.32.241.181
56232 00:02:48.427043   1130      77.66.48.87
92306 00:00:18.396753   7803    90.180.104.16

print (df_diff.dtypes)
avg_time_diff    timedelta64[ns]
count                      int64
ipAddress                 object
dtype: object

print (df_diff.info())
avg_time_diff    5 non-null timedelta64[ns]
count            5 non-null int64
ipAddress        5 non-null object
dtypes: int64(1), object(1), timedelta64[ns](1)
memory usage: 160.0+ bytes
None
print (df_diff['avg_time_diff'].dt.seconds)
22836    124
23533    215
45297    242
56232    168
92306     18
Name: avg_time_diff, dtype: int64

print (df_diff['avg_time_diff'].dt.total_seconds())
22836    124.810879
23533    215.242784
45297    242.764130
56232    168.427043
92306     18.396753
Name: avg_time_diff, dtype: float64

print (df_diff['avg_time_diff'] / np.timedelta64(1, 's'))
22836    124.810879
23533    215.242784
45297    242.764130
56232    168.427043
92306     18.396753
Name: avg_time_diff, dtype: float64

print (df_diff['avg_time_diff'].astype('timedelta64[s]'))
22836    124.0
23533    215.0
45297    242.0
56232    168.0
92306     18.0
Name: avg_time_diff, dtype: float64

转换to_timedelta

解决了问题
df_diff['avg_time_diff'] = pd.to_timedelta(df_diff['avg_time_diff'])