简体   繁体   English

pandas df 索引列表

[英]pandas df index to list

Im trying to run a pandas df index to list (time series data) with index_list = df.index.values.tolist() and it will output data like this:我正在尝试使用index_list = df.index.values.tolist()运行 pandas df索引以列出(时间序列数据),它将 output 数据如下所示:

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]

Is it possible in Pandas to output the pandas time series index to a format like this below which is like a list of time tuples if I understand the data structure correctly?如果我正确理解数据结构,是否有可能在 Pandas 到 output 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)]

I suppose your index is a DatetimeIndex so you can't obtain exactly what you want but is possible to have a similar result:我想你的索引是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)]

It's not possible because the repr of a time object is different of constructor:这是不可能的,因为时间 object 的repr与构造函数不同:

import datetime

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

# Output
datetime.time(11, 14, 15)

Update更新

To get exactly what you want, you have to define a class as proxy then override the __repr__ method:要准确获得您想要的,您必须将 class 定义为代理,然后覆盖__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]

Output: Output:

>>> 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)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM