我正在尝试处理一些传感器数据,对于这些数据,本地时间的分析对分析至关重要。请参阅以下示例分析(由上一个问题中@Alloz提供的答案提供)。带有指定时区的这段代码遇到了模糊的时间错误。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
dates = pd.date_range(start='2018-10-20', end='2018-11-05', freq='min', tz='US/Central')
vals = np.random.rand(len(dates))
df = pd.DataFrame(data={'dates': dates, 'vals': vals})
df.set_index('dates', inplace=True)
df1 = (df.groupby([np.where(df.index.weekday < 5, 'weekday', 'weekend'),
df.index.floor('10min').time])
.mean()
.rename(columns={'vals': 'average'}))
fig, ax = plt.subplots(figsize=(12,7))
df1.unstack(0).plot(ax=ax)
# Plot Full Average
df.groupby(df.index.floor('10min').time).mean().rename(columns={'vals': 'average'}).plot(ax=ax)
plt.show()
(下面是完整的追溯)
df.index.floor
调用失败,并显示以下消息:
AmbiguousTimeError: Cannot infer dst time from '2018-11-04 01:00:00', try using the 'ambiguous' argument
处理此问题的适当方法是什么?我想可以尝试滤除不明确的时间戳,而忽略它们。最好的方法是什么,或者有更好的方法来处理这种情况?
---------------------------------------------------------------------------
AmbiguousTimeError Traceback (most recent call last)
<ipython-input-27-31fa6e75660b> in <module>
1 df1 = (df.groupby([np.where(df.index.weekday < 5, 'weekday', 'weekend'),
----> 2 df.index.floor('10min').time])
3 .mean()
4 .rename(columns={'vals': 'average'}))
5
C:\Miniconda3\lib\site-packages\pandas\core\indexes\datetimelike.py in floor(self, freq)
200 @Appender((_round_doc + _floor_example).format(op="floor"))
201 def floor(self, freq):
--> 202 return self._round(freq, np.floor)
203
204 @Appender((_round_doc + _ceil_example).format(op="ceil"))
C:\Miniconda3\lib\site-packages\pandas\core\indexes\datetimelike.py in _round(self, freq, rounder)
192 attribs['tz'] = None
193 return self._ensure_localized(
--> 194 self._shallow_copy(result, **attribs))
195
196 @Appender((_round_doc + _round_example).format(op="round"))
C:\Miniconda3\lib\site-packages\pandas\core\indexes\datetimelike.py in _ensure_localized(self, result)
350 if not isinstance(result, ABCIndexClass):
351 result = self._simple_new(result)
--> 352 result = result.tz_localize(self.tz)
353 return result
354
C:\Miniconda3\lib\site-packages\pandas\core\indexes\datetimes.py in tz_localize(self, tz, ambiguous, errors)
2384 new_dates = conversion.tz_localize_to_utc(self.asi8, tz,
2385 ambiguous=ambiguous,
-> 2386 errors=errors)
2387 new_dates = new_dates.view(_NS_DTYPE)
2388 return self._shallow_copy(new_dates, tz=tz)
pandas\_libs\tslibs\conversion.pyx in pandas._libs.tslibs.conversion.tz_localize_to_utc()
AmbiguousTimeError: Cannot infer dst time from '2018-11-04 01:00:00', try using the 'ambiguous' argument
which is the moment when the clocks fall back an hour for Daylight savi