pandas ;用MM:SS转换列,十进制转换成秒数 [英] Pandas; transform column with MM:SS,decimals into number of seconds
问题描述
嘿:花了几个小时尝试做一件很简单的事情,但无法弄清楚.
Hey: Spent several hours trying to do a quite simple thing,but couldnt figure it out.
我有一个带有df ['Time']列的数据框,其中包含从0开始到20分钟的时间,像这样:
I have a dataframe with a column, df['Time'] which contains time, starting from 0, up to 20 minutes,like this:
1:10,10
1:16,32
3:03,04
第一个是分钟,第二个是秒,第三个是毫秒(只有两位数字).
First being minutes, second is seconds, third is miliseconds (only two digits).
是否有一种方法可以通过Pandas自动将该列转换为秒,而无需将该列作为系列的时间索引?
Is there a way to automatically transform that column into seconds with Pandas, and without making that column the time index of the series?
我已经尝试了以下方法,但无法正常工作:
I already tried the following but it wont work:
pd.to_datetime(df['Time']).convert('s') # AttributeError: 'Series' object has no attribute 'convert'
如果唯一的方法是解析时间,只需指出,我将为这个问题准备适当/详细的答案,请不要浪费您的时间=) 谢谢!
If the only way is to parse the time just point that out and I will prepare a proper / detailed answer to this question, dont waste your time =) Thank you!
推荐答案
代码:
import pandas as pd
import numpy as np
import datetime
df = pd.DataFrame({'Time':['1:10,10', '1:16,32', '3:03,04']})
df['time'] = df.Time.apply(lambda x: datetime.datetime.strptime(x,'%M:%S,%f'))
df['timedelta'] = df.time - datetime.datetime.strptime('00:00,0','%M:%S,%f')
df['secs'] = df['timedelta'].apply(lambda x: x / np.timedelta64(1, 's'))
print df
输出:
Time time timedelta secs
0 1:10,10 1900-01-01 00:01:10.100000 00:01:10.100000 70.10
1 1:16,32 1900-01-01 00:01:16.320000 00:01:16.320000 76.32
2 3:03,04 1900-01-01 00:03:03.040000 00:03:03.040000 183.04
如果您的时间增量也为负:
If you have also negative time deltas:
import pandas as pd
import numpy as np
import datetime
import re
regex = re.compile(r"(?P<minus>-)?((?P<minutes>\d+):)?(?P<seconds>\d+)(,(?P<centiseconds>\d{2}))?")
def parse_time(time_str):
parts = regex.match(time_str)
if not parts:
return
parts = parts.groupdict()
time_params = {}
for (name, param) in parts.iteritems():
if param and (name != 'minus'):
time_params[name] = int(param)
time_params['milliseconds'] = time_params['centiseconds']*10
del time_params['centiseconds']
return (-1 if parts['minus'] else 1) * datetime.timedelta(**time_params)
df = pd.DataFrame({'Time':['-1:10,10', '1:16,32', '3:03,04']})
df['timedelta'] = df.Time.apply(lambda x: parse_time(x))
df['secs'] = df['timedelta'].apply(lambda x: x / np.timedelta64(1, 's'))
print df
输出:
Time timedelta secs
0 -1:10,10 -00:01:10.100000 -70.10
1 1:16,32 00:01:16.320000 76.32
2 3:03,04 00:03:03.040000 183.04
这篇关于 pandas ;用MM:SS转换列,十进制转换成秒数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!