originally Date column is a string type without any null value.
import pandas as pd
# Import the dataframe
eq = pd.read_csv('Ch03_Earthquake_database.csv')
# Describe the dataframe
eq.describe()
eq['Date'].dtypes
dtype('O')
use pd.to_datetime() to convert string into datetime, why after using to_datetime(), the data type is still a string instead of datetime? Thanks
eq['Date']=pd.to_datetime(eq['Date'])
eq['Date'].dtypes
dtype('O')
then I also use dt.year to get year value
eq['Date'].dt.year
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-43-d248f5bb9e47> in <module>
----> 1 eq['Date'].dt.year
~\miniconda3\envs\forecast\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
5459 or name in self._accessors
5460 ):
-> 5461 return object.__getattribute__(self, name)
5462 else:
5463 if self._info_axis._can_hold_identifiers_and_holds_name(name):
~\miniconda3\envs\forecast\lib\site-packages\pandas\core\accessor.py in __get__(self, obj, cls)
178 # we're accessing the attribute of the class, i.e., Dataset.geo
179 return self._accessor
--> 180 accessor_obj = self._accessor(obj)
181 # Replace the property with the accessor object. Inspired by:
182 # https://www.pydanny.com/cached-property.html
~\miniconda3\envs\forecast\lib\site-packages\pandas\core\indexes\accessors.py in __new__(cls, data)
492 return PeriodProperties(data, orig)
493
--> 494 raise AttributeError("Can only use .dt accessor with datetimelike values")
AttributeError: Can only use .dt accessor with datetimelike values