简体   繁体   中英

How to validate Pandas Dataframe column consist of month-end dates?

I have a Pandas DF that has a column called ref_date which consists of dates. I want to verify that all the dates are the last day of the month. I'm trying to do it this way, but it doesn't seem to give me the correct results, particularly the df[ref_date].dt.is_month_end part is not giving me a correct Series.

df[ref_date] = pd.to_datetime(df[ref_date])

month_end_dates = df[ref_date].dt.is_month_end

indices = np.where(month_end_dates == False)[0]

if indices.size > 0:
    idx = indices[0]
    raise ValidationError("The following date is not the end of a month: " + str(df[ref_date][idx].strftime('%m/%d/%Y')))

Any suggestions/help is greatly appreciated.

EDIT: Here is an example:

df (when outputted):

    ref_date      regime_tag
0  2010-01-31           3
1  2010-02-28           2
2  2010-03-31           1
3  2010-04-30           2
4  2010-05-31           1
5  2010-06-30           1
6  2010-07-31           4
7  2010-08-31           1
8  2010-09-30           2
9  2010-10-29           4
10 2010-11-30           3
11 2010-12-31           3

month_end_dates (when outputted):

0     False
1     False
2      True
3      True
4      True
5      True
6     False
7      True
8      True
9      True
10     True
11     True

This is not right because 31 Jan 2010 is a month end and 29 Oct 2010 is not.

ANSWER:

    month_end_dates = df.ref_date + pd.offsets.MonthEnd(0) == df.ref_date

indices = np.where(month_end_dates == False)[0]

if indices.size > 0:
    idx = indices[0]
    raise ValidationError("The following date is not the end of a month: " + str(df[ref_date][idx].strftime('%m/%d/%Y')))

You can use pd.offsets.MonthEnd

df.ref_date + pd.offsets.MonthEnd(0) == df.ref_date

0      True
1      True
2      True
3      True
4      True
5      True
6      True
7      True
8      True
9     False
10     True
11     True
Name: ref_date, dtype: bool

我会建议

df.ref_date.apply(lambda x: True if x.is_month_end else False)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM