I am trying to do test to see if a data type is 'datetime.time' and if so convert it to 'datetime.datetime'. My code snippet is below. x_values is a series and each element of the series is a 'datetime.time'.
...
x_values = x.loc[:, "processed_time"]
print(x_values.dtypes)
print(type(x_values.iloc[0]))
print(x_values)
if isinstance(x_values.iloc[0], datetime.time):
x_values = pd.to_datetime(x_values, format='%H:%M:%S')
...
But the program errors out at the test with:
Traceback (most recent call last):
File "/Users/.../risk_calculations.py", line 282, in plot_risk
if isinstance(x_values.iloc[0], datetime.time):
TypeError: isinstance() arg 2 must be a type or tuple of types
object
<class 'datetime.time'>
1387 00:55:14
1388 10:02:01
1389 10:02:02
1390 10:02:02
1391 10:02:08
...
6417 14:36:49
6418 14:36:51
6419 15:24:52
6420 15:36:59
6422 16:21:03
Name: processed_time, Length: 3621, dtype: object
This Stack answer seemed closest to addressing my challenge but I think I have implemented the suggestion correctly. Note that the print statements show that the type is in fact a 'class datetime.time' as required (I think) by 'is instance' so I don't understand why the errors. I know I can make it work if I replace the 'if' statement with:
if 'datetime.time' in str(type(x_values.iloc[0])):
...
But that seems kludgy. Is there a more correct test for an instance of 'datetime.time'?