Courses in Machine Learning and Data Science - Analytics Vidhya

My code is not running and triggers following error,
Code:

for i in (train, test, test_original, train_original):
    i['year']=i.Datetime.dt.year
    i['month']=i.Datetime.dt.month 
    i['day']=i.Datetime.dt.day
    i['Hour']=i.Datetime.dt.hour

Error:

AttributeError Traceback (most recent call last)
in
2 # So, let’s extract the year, month, day and hour from the Datetime to validate our hypothesis.
3 for i in (train, test, test_original, train_original):
----> 4 i[‘year’]=i.Datetime.dt.year
5 i[‘month’]=i.Datetime.dt.month
6 i[‘day’]=i.Datetime.dt.day

~/anaconda3/lib/python3.7/site-packages/pandas/core/generic.py in getattr(self, name)
5061 if (name in self._internal_names_set or name in self._metadata or
5062 name in self._accessors):
-> 5063 return object.getattribute(self, name)
5064 else:
5065 if self._info_axis._can_hold_identifiers_and_holds_name(name):

~/anaconda3/lib/python3.7/site-packages/pandas/core/accessor.py in get(self, obj, cls)
169 # we’re accessing the attribute of the class, i.e., Dataset.geo
170 return self._accessor
–> 171 accessor_obj = self._accessor(obj)
172 # Replace the property with the accessor object. Inspired by:
173 # http://www.pydanny.com/cached-property.html

~/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/accessors.py in new(cls, data)
322 pass # we raise an attribute error anyway
323
–> 324 raise AttributeError("Can only use .dt accessor with datetimelike "
325 “values”)

AttributeError: Can only use .dt accessor with datetimelike values

Hi @monipriyanka03,

Looks like you have not converted your given variable into the Datetime format. Please print the dtype of the variable.

© Copyright 2013-2019 Analytics Vidhya