'Cannot convert to datetime object
My task:
Import Counter from collections and datetime from datetime. Create a Counter object called crimes_by_month. Loop over the crime_data list:
Using the datetime.strptime() function, convert the first element of each item into a Python Datetime Object called date. Increment the counter for the month associated with this row by one. You can access the month of date using date.month.
Print the 3 most common months for crime.
My code:
print(crime_data[:10])
[('05/23/2016 05:35:00 PM', 'ASSAULT', 'STREET', 'false'), ('03/26/2016 08:20:00 PM',
'BURGLARY', 'SMALL RETAIL STORE', 'false'), ('04/25/2016 03:05:00 PM', 'THEFT', 'DEPARTMENT
STORE', 'true'), ('04/26/2016 05:30:00 PM', 'BATTERY', 'SIDEWALK', 'false'), ('06/19/2016
01:15:00 AM', 'BATTERY', 'SIDEWALK', 'false'), ('05/28/2016 08:00:00 PM', 'BATTERY', 'GAS
STATION', 'false'), ('07/03/2016 03:43:00 PM', 'THEFT', 'OTHER', 'false'), ('06/11/2016
06:55:00 PM', 'PUBLIC PEACE VIOLATION', 'STREET', 'true'), ('10/04/2016 10:20:00 AM',
'BATTERY', 'STREET', 'true'), ('02/14/2017 09:00:00 PM', 'CRIMINAL DAMAGE', 'PARK PROPERTY',
'false')]
# Import necessary modules
from collections import Counter
from datetime import datetime
# Create a Counter Object: crimes_by_month
crimes_by_month = collections.Counter()
# Loop over the crime_data list
for date in crime_data:
# Convert the first element of each item into a Python Datetime Object: date
date = datetime.strptime(date[0], '%m/%d/%Y %I:%M:%S %p')
# Increment the counter for the month of the row by one
crimes_by_month(key = date.month) += 1
# Print the 3 most common months for crime
print(something_count.most_common(3))
I get two mistakes ''int' object has no attribute 'items'' and 'cannot assign to function call'
Expected result: [(1, 1948), (2, 1862), (7, 1257)]
Solution 1:[1]
You only had a few issues. To increment a Counter, you use dictionary notation:
crimes_by_month[date.month] += 1
You then did
from collections import Counter
...
crimes_by_month = collections.Counter()
Because you specifically imported the name, you don't need the module name here. Just use Counter().
Here is your code, working:
crime_data = [
('05/23/2016 05:35:00 PM', 'ASSAULT', 'STREET', 'false'),
('03/26/2016 08:20:00 PM', 'BURGLARY', 'SMALL RETAIL STORE', 'false'),
('04/25/2016 03:05:00 PM', 'THEFT', 'DEPARTMENT STORE', 'true'),
('04/26/2016 05:30:00 PM', 'BATTERY', 'SIDEWALK', 'false'),
('06/19/2016 01:15:00 AM', 'BATTERY', 'SIDEWALK', 'false'),
('05/28/2016 08:00:00 PM', 'BATTERY', 'GAS STATION', 'false'),
('07/03/2016 03:43:00 PM', 'THEFT', 'OTHER', 'false'),
('06/11/2016 06:55:00 PM', 'PUBLIC PEACE VIOLATION', 'STREET', 'true'),
('10/04/2016 10:20:00 AM', 'BATTERY', 'STREET', 'true'),
('02/14/2017 09:00:00 PM', 'CRIMINAL DAMAGE', 'PARK PROPERTY', 'false')
]
# Import necessary modules
from collections import Counter
from datetime import datetime
# Create a Counter Object: crimes_by_month
crimes_by_month = Counter()
# Loop over the crime_data list
for date in crime_data:
# Convert the first element of each item into a Python Datetime Object: date
date = datetime.strptime(date[0], '%m/%d/%Y %I:%M:%S %p')
# Increment the counter for the month of the row by one
crimes_by_month[date.month] += 1
# Print the 3 most common months for crime
print(crimes_by_month.most_common(3))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tim Roberts |
