How can I check if a pandas series contains timestamps?
2 answers
try this, it should work:
pd.core.dtypes.common.is_datetime_or_timedelta_dtype(series)
if you go through pd.core.dtypes.common.is_
you will find many options for checking timestamps. If you want to combine them, you can use boolean operators, for example:
pd.core.dtypes.common.is_datetime64_ns_dtype(ser)|pd.core.dtypes.common.is_timedelta64_ns_dtype(ser)
+3
source to share
The approach in @ shivsn's answer is simpler and most likely better. This is more difficult, but can be informative nonetheless.
More about dtypes see. Here . But in a nutshell, the second character of the dtype string should be "M" for Datetime and "m" for Timedelta. Hence, you can test dtype.str[1] == 'M'
to detect only Datetime, or dtype.str[1].lower() == 'm'
to detect either Datetime or Timedelta.
>>> dr = pd.date_range('1-1-2017',periods=4,freq='d')
>>> df = pd.DataFrame({ 'i':range(3), 'x':[1.1,2.2,3.3],
'ts':dr[:3], 'td':dr[1:]-dr[:3] })
i td ts x
0 0 1 days 2017-01-01 1.1
1 1 1 days 2017-01-02 2.2
2 2 1 days 2017-01-03 3.3
>>> for v in df.columns:
print( '\ncolumn ' + v + ': ')
print( 'dtype.str: ', df[v].dtype.str )
print( 'dtype: ', df[v].dtype )
print( 'timestamp? ', df[v].dtype.str[1] == 'M' )
column i:
dtype.str: <i8
dtype: int64
timestamp? False
column td:
dtype.str: <m8[ns]
dtype: timedelta64[ns]
timestamp? False
column ts:
dtype.str: <M8[ns]
dtype: datetime64[ns]
timestamp? True
column x:
dtype.str: <f8
dtype: float64
timestamp? False
+2
source to share