Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

timedelta64[ns] -> FutureWarning: Passing timedelta64-dtype data is deprecated, will raise a TypeError in a future version

Assuming df['time'] is from type timedelta64[ns] and df['a'] as well as df['b'] are from type float64, the two series can be plotted like this:

import pandas as pd
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2, sharex=True)

time_values = pd.to_datetime(df['time'])
axs[0].plot(time_values, df['a'])
axs[1].plot(time_values, df['b'])
plt.show()

This works.. But gives the following Warning:

FutureWarning: Passing timedelta64-dtype data is deprecated, will raise a TypeError in a future version

So what should be used instead of pd.to_datetime to display timedelta64[ns] as human-readable time in matplotlib?

like image 594
gies0r Avatar asked Nov 17 '22 05:11

gies0r


1 Answers

I faced the same issue when reading time data from SQL using pandas. These 2 lines solved my problem. I tried to find another direct solution without success.

time_values = df['time'].apply(lambda x: np.nan if pd.isnull(x) else str(x)[-8:])
time_values = pd.to_datetime(time_values, format='%H:%M:%S').dt.time
like image 189
Razmik Melikbekyan Avatar answered Dec 29 '22 20:12

Razmik Melikbekyan