Confused on Index Slicing in python

119 views Asked by At

I am slicing my hour column and when I use slice(1, 3) and slice(0, 3) I get the same results. What am i missing? See in the image below.

Original Column

slice(0, 3)

slice(1, 3)

2

There are 2 answers

0
Meet Shah On BEST ANSWER

There is a space at the beginning of dataset["hour"].

So slice(0, 3) is " 02" while slice(1, 3) is "02".

This was answered by @Barmar on comment.

0
Alexandra Dudkina On

If it's needed to extract an hour from the time, it probably would be better to use:

dataset['hour'] = pd.to_datetime(dataset['hour'],format= '%H:%M:%S' ).dt.hour