Pandas Gives Incorrect Result When Asking If Timestamp Column Values Have Attr Astype
With a column containing Timestamp values, I am getting inconsistent results about whether the elements have the attribute astype: In [30]: o.head().datetime.map(lambda x: hasattr(
Solution 1:
Timestamps do not have an astype method. But numpy.datetime64's do.
NDFrame.values
returns a numpy array.o.head().datetime.values
returns a numpy array of dtype numpy.datetime64
, which is why
In [31]: map(lambda x: hasattr(x, 'astype'), o.head().datetime.values)
Out[31]: [True, True, True, True, True]
Note that Series.__iter__
is defined this way:
def__iter__(self):
if com.is_categorical_dtype(self.dtype):
returniter(self.values)
elif np.issubdtype(self.dtype, np.datetime64):
return (lib.Timestamp(x) for x in self.values)
elif np.issubdtype(self.dtype, np.timedelta64):
return (lib.Timedelta(x) for x in self.values)
else:
returniter(self.values)
So, when the dtype of the Series is np.datetime64
, iteration over the Series
returns Timestamps. This is where the implicit conversion takes place.
Post a Comment for "Pandas Gives Incorrect Result When Asking If Timestamp Column Values Have Attr Astype"