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: [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?
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)]
CodePudding user response:
I suppose your index is a DatetimeIndex
so you can't obtain exactly what you want but is possible to have a similar result:
>>> 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:
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:
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:
>>> 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)]