Home > database >  check sorting by year and quarter pandas dataframe
check sorting by year and quarter pandas dataframe

Time:03-14

I have a df that looks like below

     date      col1    col2
 0  2000 Q1    123     456
 1  2000 Q2    234     567
 2  2000 Q3    345     678
 3  2000 Q4    456     789
 4  2001 Q1    567     890

The df has over 200 rows. I need to -

  1. check if the data is sorted by date
  2. if not, then sort it by date

Can someone please help me with this?

Many thanks

CodePudding user response:

Use DataFrame.sort_values with key parameter and converting values to datetimes:

df = df.sort_values('date', key=lambda x: pd.to_datetime(x.str.replace('\s ', '')))
print (df)
      date  col1  col2
0  2000 Q1   123   456
1  2000 Q2   234   567
2  2000 Q3   345   678
3  2000 Q4   456   789
4  2001 Q1   567   890

EDIT: You can use Series.is_monotonic for test if values are monotonic_increasing:

if not df['date'].is_monotonic:
    df = df.sort_values('date', key=lambda x: pd.to_datetime(x.str.replace('\s ', '')))

CodePudding user response:

You can convert your date column as pd.Index (or define it as the index of your dataframe):

if not pd.Index(df['date']).is_monotonic_increasing:
    df = df.sort_values('date')
  • Related