为什么包含不能 select 行包含指定的字符串?

Why contains can't select rows contains specified string?

>>> y
1    2002-12-31
2    2003-12-31
3    2004-03-31
4    2004-06-30
Name: report_date, dtype: object

我想提取包含 12-31 的行。

>>> y.str.contains('12-31')
>>> y.str.contains('\.+12-31')
>>> y.str.contains('2002-12-31')

所有三个表达式得到相同的输出:

1   NaN
2   NaN
3   NaN
4   NaN
Name: report_date, dtype: float64

如何提取包含字符串 12-31 的行? 我想要的输出:

1   True
2   True
3   NaN
4   NaN

可能列中是日期,所以将其转换为字符串之前:

m = y.astype(str).str.contains('12-31')
print (m)
0     True
1     True
2    False
3    False
Name: report_date, dtype: bool

也许使用日期时间并检查月份和日期字段:

df['report_date'] = pd.to_datetime(df.report_date)

df[df.report_date.dt.month.eq(12) & df.report_date.dt.day.eq(31)]

report_date
1  2002-12-31
2  2003-12-31

我愿意

y.dt.strftime('%m-%d')=='12-31'