0

I have a pandas dataframe filled with time-stamped data. It is out of order; and I am trying to sort by date, hours and minutes. The pandas dataframe will organize by date, but not by hours and minutes.

My dataframe is loaded in ('df'), and the column 'dttime' was changed it into a dateframe from integer numbers.

df['dttime'] = pd.to_datetime(df['dttime'], format='%y%m%d%H%M%S')

I resort it with:

df.sort_values(by='dttime')    

but that does not seem to have the right ordering of the hour minutes and seconds.

Eri
  • 3
  • 2
  • 1
    Please post some data so we can reproduce the issue. See [mre] and [How to make good reproducible pandas examples](/q/20109391/4518341). BTW, welcome to Stack Overflow! Check out the [tour], and [ask] if you want tips. – wjandrea Nov 03 '22 at 04:23

1 Answers1

1

I tried with some dummy data and it doesn't look like an issue to me. Please check the below code.

import pandas as pd
data = ['221011141200', '221011031200', '221011191200', '221011131600']

df = pd.DataFrame(data, columns=['dttime'])
df['dttime'] = pd.to_datetime(df['dttime'], format='%y%m%d%H%M%S')

# Before sorting
print(df)

# After sorting
df = df.sort_values(by='dttime')
print(df)

Output is as follows:

               dttime
0 2022-10-11 14:12:00
1 2022-10-11 03:12:00
2 2022-10-11 19:12:00
3 2022-10-11 13:16:00

               dttime
1 2022-10-11 03:12:00
3 2022-10-11 13:16:00
0 2022-10-11 14:12:00
2 2022-10-11 19:12:00
Abhi
  • 1,080
  • 1
  • 7
  • 21
  • Yes, sorting does work on Timestamps dtype. – n3a5p7s9t1e3r Nov 03 '22 at 06:37
  • 1
    Thank you, this worked! I think the issue was I was just writing 'df.sort_values(by='dttime')' instead of 'df = df.sort_values(by='dttime')' so I wasn't actually redefining anything! – Eri Nov 03 '22 at 16:51