PandasDF索引到列表

icomxhvb  于 2023-01-28  发布在  其他
关注(0)|答案(1)|浏览(146)

我试图运行一个Pandasdf***index***to list(时间序列数据)与index_list = df.index.values.tolist(),它会输出如下数据:

index_list:  [1674576900000000000, 1674577800000000000, 1674578700000000000, 1674579600000000000, 1674580500000000000, 1674581400000000000, 1674582300000000000, 1674583200000000000, 1674584100000000000, 1674585000000000000, 1674585900000000000, 1674586800000000000, 1674587700000000000, 1674588600000000000, 1674589500000000000, 1674590400000000000, 1674591300000000000, 1674592200000000000, 1674593100000000000, 1674594000000000000, 1674594900000000000, 1674595800000000000, 1674596700000000000, 1674597600000000000, 1674598500000000000, 1674599400000000000, 1674600300000000000, 1674601200000000000, 1674602100000000000, 1674603000000000000, 1674603900000000000, 1674604800000000000, 1674605700000000000, 1674606600000000000, 1674607500000000000, 1674608400000000000, 1674609300000000000, 1674610200000000000, 1674611100000000000, 1674612000000000000, 1674612900000000000, 1674613800000000000, 1674614700000000000, 1674615600000000000, 1674616500000000000, 1674617400000000000, 1674618300000000000, 1674619200000000000, 1674620100000000000, 1674621000000000000, 1674621900000000000, 1674622800000000000, 1674623700000000000, 1674624600000000000, 1674625500000000000, 1674626400000000000, 1674627300000000000, 1674628200000000000, 1674629100000000000, 1674630000000000000, 1674630900000000000, 1674631800000000000, 1674632700000000000, 1674633600000000000, 1674634500000000000, 1674635400000000000, 1674636300000000000, 1674637200000000000, 1674638100000000000, 1674639000000000000, 1674639900000000000, 1674640800000000000, 1674641700000000000, 1674642600000000000, 1674643500000000000, 1674644400000000000, 1674645300000000000, 1674646200000000000, 1674647100000000000, 1674648000000000000, 1674648900000000000, 1674649800000000000, 1674650700000000000, 1674651600000000000, 1674652500000000000, 1674653400000000000, 1674654300000000000, 1674655200000000000, 1674656100000000000, 1674657000000000000, 1674657900000000000, 1674658800000000000, 1674659700000000000, 1674660600000000000, 1674661500000000000, 1674662400000000000]

如果我正确理解了数据结构,在Pandas中是否可以将Pandas的时间序列***索引***输出为如下格式,类似于时间元组列表?

index_list= [time(hour=11, minute=14, second=15),
         time(hour=11, minute=14, second=30),
         time(hour=11, minute=14, second=45),
         time(hour=11, minute=15, second=00),
         time(hour=11, minute=15, second=15),
         time(hour=11, minute=15, second=30),
         time(hour=11, minute=15, second=45),
         time(hour=11, minute=16, second=00),
         time(hour=11, minute=16, second=15),
         time(hour=11, minute=16, second=30),
         time(hour=11, minute=16, second=45),
         time(hour=11, minute=17, second=00),
         time(hour=11, minute=17, second=15),
         time(hour=11, minute=17, second=30),
         time(hour=11, minute=17, second=45),
         time(hour=11, minute=18, second=00),
         time(hour=11, minute=18, second=15),
         time(hour=11, minute=18, second=30)]
t5fffqht

t5fffqht1#

我假设你的索引是DatetimeIndex,所以你不能得到你想要的东西,但是可能会得到类似的结果:

>>> df.index.time.tolist() 
[datetime.time(16, 15),
 datetime.time(16, 30),
 datetime.time(16, 45),
 datetime.time(17, 0),
 datetime.time(17, 15),
 ...
 datetime.time(15, 0),
 datetime.time(15, 15),
 datetime.time(15, 30),
 datetime.time(15, 45),
 datetime.time(16, 0)]

这是不可能的,因为时间对象的repr与构造函数不同:

import datetime

t = datetime.time(hour=11, minute=14, second=15)
print(t)

# Output
datetime.time(11, 14, 15)
    • 更新**

为了得到你想要的,你必须定义一个类作为代理,然后覆盖__repr__方法:

import datetime

class time:
    def __init__(self, hour=0, minute=0, second=0):
        self.t = datetime.time(hour, minute, second)

    def __repr__(self):
        return f'time(hour={self.t.hour}, minute={self.t.minute}, second={self.t.second})'

new_list = [time(t.hour, t.minute, t.second) for t in df.index.time]

输出:

>>> new_list
[time(hour=16, minute=15, second=0),
 time(hour=16, minute=30, second=0),
 time(hour=16, minute=45, second=0),
 time(hour=17, minute=0, second=0),
 time(hour=17, minute=15, second=0),
 ...
 time(hour=15, minute=0, second=0),
 time(hour=15, minute=15, second=0),
 time(hour=15, minute=30, second=0),
 time(hour=15, minute=45, second=0),
 time(hour=16, minute=0, second=0)]

相关问题