Python,Pandas,将Timestamp转换为Period

时间:2017-01-16 10:26:49

标签: python pandas

让我们考虑以下DataFrame:

date_range = pd.date_range(dt(2010,1,1), dt(2010,1,31), freq='1D')
df = pd.DataFrame(data = np.random.rand(len(date_range),2), index = date_range)

如果我按照1周的时间对数据点进行分组并可视化组定义,我会得到:

In: [1]:df.groupby(pd.TimeGrouper('W')).groups
Out:[1]: 
     {Timestamp('2010-01-03 00:00:00', freq='W-SUN'): 3,
     Timestamp('2010-01-10 00:00:00', freq='W-SUN'): 10,
     Timestamp('2010-01-17 00:00:00', freq='W-SUN'): 17,
     Timestamp('2010-01-24 00:00:00', freq='W-SUN'): 24,
     Timestamp('2010-01-31 00:00:00', freq='W-SUN'): 31}

我检索该词典的键:

In: [2]: list(df.groupby(pd.TimeGrouper('W')).keys())
Out:[2]: 
    [Timestamp('2010-01-03 00:00:00', freq='W-SUN'),
     Timestamp('2010-01-10 00:00:00', freq='W-SUN'),
     Timestamp('2010-01-31 00:00:00', freq='W-SUN'),
     Timestamp('2010-01-17 00:00:00', freq='W-SUN'),
     Timestamp('2010-01-24 00:00:00', freq='W-SUN')]

但是我留下了那些有趣的变量,例如Timestamp('2010-01-24 00:00:00', freq='W-SUN'),其前缀为Timestamp但结构类似于句号。如何将这些值转换为实际的期间?

1 个答案:

答案 0 :(得分:1)

您可以使用:

a = df.groupby(pd.TimeGrouper('W')).groups
per = [pd.Period(x, freq=x.freq) for x in a]
print (per)
[Period('2010-01-18/2010-01-24', 'W-SUN'), 
 Period('2010-01-04/2010-01-10', 'W-SUN'), 
 Period('2010-01-25/2010-01-31', 'W-SUN'), 
Period('2010-01-11/2010-01-17', 'W-SUN'), 
Period('2009-12-28/2010-01-03', 'W-SUN')]