0

I want to look at specific dates (zoom in for closer look) for June months, but run into a problem when filtering the dates from 2000-06 to 2014-06 to plot as a scatter.

I've taken a look at this thread: Select DataFrame rows between two dates but none of the solutions using .loc[] or mask seemed to work for my problem. Subsetting the df seems to have worked as mentioned by @r-beginner.

Here is what I have so far:

earthquake_idx['time'] = pd.to_datetime(earthquake_idx.index, utc=True)
june_summer_quakes = earthquake_idx[earthquake_idx['time'].dt.month == 6]

Output:

Index(['2013-06-07 18:33:11.19-04', '2012-06-17 
    13:40:31.7-04',
   '2014-06-30 19:09:57.7-04', '2014-06-30 17:00:24.06- 
    04',
   '2014-06-30 16:54:01.63-04', '2014-06-30 
    16:15:55.33-04',
   '2014-06-30 15:03:35.5-04', '2014-06-30 14:20:17- 
    04',
   '2014-06-30 12:49:41-04', '2014-06-30 11:03:51.6- 
    04',
   ...
   '1973-06-02 20:07:30.5-04', '1973-06-01 04:52:44.6- 
    04',
   '1973-06-01 02:42:25.2-04', '1972-06-12 19:47:37- 
    04',
   '1971-06-11 12:56:04-04', '1971-06-05 14:20:44-04',
   '1970-06-24 13:09:13-04', '1970-06-24 07:30:29-04',
   '1970-06-22 14:39:30-04', '1970-06-12 04:54:32-04'],
   Name: time, Length: 8372, dtype: datetime64[ns, UTC]

Plotting this works, but messy:

fig, ax = plt.subplots()
ax.plot(june_summer_quakes.index, 
june_summer_quakes['mag'])
ax.set_xlabel('Time (years)')
ax.set_ylabel('Magnitude')
plt.show()

June Earthquakes

I've tried:

two_thousand = june_summer_quakes.loc['2000-06- 
01':'2014-06-30']

two_thousand = june_summer_quakes['2000-06-01':'2014- 
06-30']

mask = (june_summer_quakes['time'] > '2000-06-01') & 
(june_summer_quakes['time'] <== '2014-06-30')
print(june_summer_quakes.loc[mask]

But the ouput is:

 Keyerror '2000-06-01' or invalid syntax using the mask 
 method.

What am I doing wrong?

AJ.
  • 19
  • 8
  • 2
    `two_thousand = june_summer_quakes.loc[(june_summer_quakes['time'] >= '2000-06-01') & (june_summer_quakes['time'] <= '2014-06-30')]`The extraction of the data frame is written in the way described above. – r-beginners Sep 25 '20 at 06:27
  • r-beginners, yes that worked beautifully, thank you! Could I ask why subsetting is needed again? I looked at a similar question after searching on here from the link Trenton McKinney provided and that's where i tried to use the .loc but failed. – AJ. Sep 25 '20 at 09:51
  • @TrentonMcKinney, yes I searched on here and used the ''boolean mask method", but still couldn't produce the graph. Thank you. – AJ. Sep 25 '20 at 09:58

0 Answers0