This is the plain column
JavaScript
x
6
1
0 06:55:22
2
1 06:55:23
3
2 06:55:24
4
3 06:55:25
5
4 06:55:26
6
And the I would like to put that column in the index, the problem is when I try to use the method resample() I always get the same problem:
TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of ‘Index’
I’ve been using this to change the Time column to
JavaScript
1
2
1
datetime dt['Time'] = pd.to_datetime(dt['Time'],format).apply(lambda x: x.time())
2
Advertisement
Answer
You can use set_index
to set the Time
column as your index of the dataframe.
JavaScript
1
9
1
In [1954]: df.set_index('Time')
2
Out[1954]:
3
a
4
Time
5
06:55:23 1
6
06:55:24 2
7
06:55:25 3
8
06:55:26 4
9
Update after OP’s comment
If you don’t have a date
column, so pandas will attach a default date 1900-01-01
when you convert it to datetime
. Like this:
JavaScript
1
8
1
In [1985]: pd.to_datetime(df['Time'], format='%H:%M:%S')
2
Out[1985]:
3
0 1900-01-01 06:55:23
4
1 1900-01-01 06:55:24
5
2 1900-01-01 06:55:25
6
3 1900-01-01 06:55:26
7
Name: Time, dtype: datetime64[ns]
8