Pandas groupby with regex capturing unwanted elements in series

Viewed 698

It's my first time posting, so I hope this is sufficiently documented to explain the issue I'm having.

I am working with 25-odd years of NOAA wind direction and speed data, that I have cleaned and loaded into a Pandas data frame. I was able to successfully sort the data by month, using the following to isolate my results to only July of each year:

in: july_df = df[df['date'].str.contains('\d{4}\-(7|07)', regex=True)]
in: july_df.head(5)
Out: 
             date   time  direction  speed
24732  1992-07-01  00:00      128.0    4.1
24733  1992-07-01  00:10      120.0    3.3
24734  1992-07-01  00:20      117.0    3.2
24735  1992-07-01  00:30      108.0    3.1
24736  1992-07-01  00:40      112.0    2.9

However, when I attempt to isolate the data further, grouping it by daily reading from only the month of July, I consistently get a few readings from the first day, and the number of those readings seems to change according to how many entries I want to view at a time. Here, I try to groupby July 14th:

in: july_14 = july_df.groupby(df.date.str.contains("\d{4}\-07\-14", regex=True))
in: july_14.head(5)
out: 
             date   time  direction  speed
24732  1992-07-01  00:00      128.0    4.1
24733  1992-07-01  00:10      120.0    3.3
24734  1992-07-01  00:20      117.0    3.2
24735  1992-07-01  00:30      108.0    3.1
24736  1992-07-01  00:40      112.0    2.9
26532  1992-07-14  00:00       91.0    4.3
26533  1992-07-14  00:10       82.0    4.2
26534  1992-07-14  00:20       78.0    4.6
26535  1992-07-14  00:30       73.0    4.6
26536  1992-07-14  00:40       71.0    4.2

I expected that grouping as I had previously would output only results related to July 14, which is the date I want to analyze more closely.

I think the problem is in the regex I am using. For example, if I change the input to a day not in the dataset (July 32), I still get results for July 1:

in: july_14 = july_df.groupby(df.date.str.contains("\d{4}\-07\-32", regex=True))
in: july_14.head()
out: 
             date   time  direction  speed
24732  1992-07-01  00:00      128.0    4.1
24733  1992-07-01  00:10      120.0    3.3
24734  1992-07-01  00:20      117.0    3.2
24735  1992-07-01  00:30      108.0    3.1
24736  1992-07-01  00:40      112.0    2.9

I even get this kind of wonky result when I group by year:

print july_df.groupby(july_df.date.str.contains("2001", regex=True)).head(5)
              date   time  direction  speed
24732   1992-07-01  00:00      128.0    4.1
24733   1992-07-01  00:10      120.0    3.3
24734   1992-07-01  00:20      117.0    3.2
24735   1992-07-01  00:30      108.0    3.1
24736   1992-07-01  00:40      112.0    2.9
374667  2001-07-01  00:00       96.0    4.7
374668  2001-07-01  00:10       98.0    5.1
374669  2001-07-01  00:20      100.0    5.4
374670  2001-07-01  00:30       97.0    5.3
374671  2001-07-01  00:40       83.0    5.1

Can anyone point out my mistake(s)?

Thanks for your help. Please, let me know if this is insufficiently documented or a repeat question.

1 Answers
Related