1

I am using crime statistics (in a data frame)and I am trying to find when most crimes occur between 12 am-8am,8am-4pm, and 4pm-12pm. I have already converted the column to DateTime. the code I used is:

#first attempt
df_15['FIRST_OCCURRENCE_DATE']=pd.date_range('01/01/2015',periods=10000,freq='H')
df_15[(df_15['FIRST_OCCURrENCE_DATE'] > '2015-1-1 00:00:00') & (df_15['FIRST_OCCURRENCE_DATE'] <= '2015-12-31 08:00:00')]

#second attempt
df_15 = df_15.set_index(df_15['FIRST_OCCURRENCE_DATE'])
df_15.loc['2015-01-01 00:00:00':'2015-12-31 00:00:00']

#third attempt
date_rng = pd.date_range(start='00:00:00', end='08:00:00',freq='H')
date_rng1 = pd.DataFrame(date_rng)
date_rng1.head(30)

#fourth attempt
df_15.FIRST_OCCURRENCE_DATE.dt.hour
ts = pd.to_datetime('12/31/2015 08:00:00')
df_15.loc[df_15.FIRST_OCCURRENCE_DATE <= ts,:].head()

The results I get are time entries that go outside of 08:00:00.

PS. all the data is from the same year

2
  • 1
    What are you expecting as output? Commented Oct 9, 2018 at 23:03
  • I would like to get a total count of crimes that happened in each time interval. For example 12,000 between 12am(00:00:00)-8am(08:00:00). 10,000 from 8am-4pm Commented Oct 9, 2018 at 23:08

1 Answer 1

2

Looks like you can just do a little arithmetic and count:

(df_15['FIRST_OCCURrENCE_DATE'].dt.hour // 8).value_counts()

There are a lot of ways to solve this problem but this is likely the simplest. Extract the hour of day from each date, find which time slot it belongs to. Floor-divide by 8 to get 0 (12AM-8AM), 1 (8AM-4PM), or 2 (4PM-12AM) for each, and just count these occurrences.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried that and I got a syntax error: df_15['FIRST_OCCURRENCE_DATE'].dt.hour//8).value_counts() ^ when i removed the ) it showed a syntaxerror:value_counts
@S.Caruso Sorry, that was a copy-paste error. Can you try again please

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.