This piece of code should be self-explanatory and it's also a choke-point in my process. Any way to do this better?

geoset['MonthInt'] = pd.to_datetime(geoset['Date received']).dt.month # Add a month
geoset['YearInt'] = pd.to_datetime(geoset['Date received']).dt.year # Add a year
geoset['DayInt'] = pd.to_datetime(geoset['Date received']).dt.day # Add a day of month
geoset['DayOfWeekInt'] = pd.to_datetime(geoset['Date received']).dt.dayofweek # Add a day of week

1 Answer 1


Obvious optimisation would be converting 'Date received' to pandas datetime once, for example (if you need to retain original column):

geoset['pddate_received'] = pd.to_datetime(geoset['Date received'])
geoset['MonthInt'] = geoset['pddate_received'].dt.month # Add a month
geoset['YearInt'] = geoset['pddate_received'].dt.year # Add a year
geoset['DayInt'] = geoset['pddate_received'].dt.day # Add a day of month
geoset['DayOfWeekInt'] = geoset['pddate_received'].dt.dayofweek # Add a day of week

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.