我有一个这样的数据框df:
datetime duration
0 2018-10-08 13:30:00 03:00
1 2018-10-08 16:40:00 00:11
2 2018-10-08 21:30:00 03:19
3 2018-10-09 03:21:00 04:27
4 2018-10-09 07:49:00 02:11
两列的两种类型均为pandas.core.series.Series:
In[20]: type(df_sleep['datetime'])
Out[20]: pandas.core.series.Series
In[21]: type(df_sleep['duration'])
Out[20]: pandas.core.series.Series
我想使用以下内容转换数据:
import matplotlib.dates as dates
dates.date2num(df_sleep['datetime'])
dates.date2num(df_sleep['duration'])
“日期时间”列有效时,“持续时间”列显示以下错误:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-22-3720cbfdbdfa> in <module>()
----> 1 dates.date2num(df_sleep['duration'])
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/dates.py in date2num(d)
450 if not d.size:
451 return d
--> 452 return _to_ordinalf_np_vectorized(d)
453
454
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/numpy/lib/function_base.py in __call__(self, *args, **kwargs)
2753 vargs.extend([kwargs[_n] for _n in names])
2754
-> 2755 return self._vectorize_call(func=func, args=vargs)
2756
2757 def _get_ufunc_and_otypes(self, func, args):
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/numpy/lib/function_base.py in _vectorize_call(self, func, args)
2823 res = func()
2824 else:
-> 2825 ufunc, otypes = self._get_ufunc_and_otypes(func=func, args=args)
2826
2827 # Convert args to object arrays first
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/numpy/lib/function_base.py in _get_ufunc_and_otypes(self, func, args)
2783
2784 inputs = [arg.flat[0] for arg in args]
-> 2785 outputs = func(*inputs)
2786
2787 # Performance note: profiling indicates that -- for simple
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/dates.py in _to_ordinalf(dt)
253 tzi = UTC
254
--> 255 base = float(dt.toordinal())
256
257 # If it's sufficiently datetime-like, it will have a `date()` method
AttributeError: 'str' object has no attribute 'toordinal'
有人知道吗?我的最终目标是使用Matplotlib在“ datetime”(x轴)-“ duration”(y轴)中绘制数据。我想这是因为df ['duration']列仅包含时间,而没有日期,并且无法做转换?我应该怎么做?
非常感谢您的任何建议!
答案 0 :(得分:0)
猜猜您的持续时间格式为%H:%M。首先,将列格式更改为datetime。
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter, date2num
df['datetime'] = pd.to_datetime(df.datetime)
df["duration"] = pd.to_datetime(df["duration"],format="%H:%M")
fig, ax = plt.subplots()
myFmt = DateFormatter("%H:%M:%S")
ax.yaxis.set_major_formatter(myFmt)
ax.plot(df['datetime'], df['duration'])
plt.gcf().autofmt_xdate()
plt.show()