1

My DataFrame looks like this:

    Category       Date
81    Monate 2020-01-01
88    Monate 2020-01-02
58    Monate 2020-01-03
3     Monate 2020-01-04
23    Monate 2020-01-05
..       ...        ...
134   Wochen 2020-05-24
145     Tage 2020-05-25
147     Tage 2020-05-26
146     Tage 2020-05-27
148     Tage 2020-05-28

It is ordered by Date. I need to run a check if on each row Monate follows Monate, Wochen follows Wochen and so on. It is allowed that Wochen follows Monate and Tage follows Wochen. I hope it is clear that I mean. Something looks this should cause an error, since the sequence is invalid.

    Category       Date
81    Monate 2020-01-01
88    Monate 2020-01-02
58    Tage   2020-01-03
3     Monate 2020-01-04
23    Monate 2020-01-05
..       ...        ...
134   Wochen 2020-05-24
145     Tage 2020-05-25
147     Tage 2020-05-26
146   Wochen 2020-05-27
148     Tage 2020-05-28

I could try to write a pretty complicated and probably slow iteration over each row.

for row in result_df.iterrows():
    do xyz

Is there a better and quicker way to check for an ongoing sequence in a Series or a maybe in a list, dictionary etc.?

1 Answer 1

2

I believe you can create a numeric dictionary stating the order and replace the values of the Category column and check if series.diff is never negative with series.all:

def check(dataframe):
    d = {'Monate':1,'Wochen':2,'Tage':3}
    return dataframe['Category'].replace(d).diff().fillna(0).ge(0).all()

Test Runs:

print(df,'\n\n',f"Valid? : {check(df)}",'\n\n',df1,'\n\n',f"Valid? : {check(df1)}")

 Category        Date
81    Monate  2020-01-01
88    Monate  2020-01-02
58    Monate  2020-01-03
3     Monate  2020-01-04
23    Monate  2020-01-05
134   Wochen  2020-05-24
145     Tage  2020-05-25
147     Tage  2020-05-26
146     Tage  2020-05-27
148     Tage  2020-05-28 

 Valid? : True 

     Category        Date
81    Monate  2020-01-01
88    Monate  2020-01-02
58      Tage  2020-01-03
3     Monate  2020-01-04
23    Monate  2020-01-05
134   Wochen  2020-05-24
145     Tage  2020-05-25
147     Tage  2020-05-26
146   Wochen  2020-05-27
148     Tage  2020-05-28 

 Valid? : False
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.