代码之家  ›  专栏  ›  技术社区  ›  Vince W.

用pandas解决时间序列分析中的模糊时间误差

  •  0
  • Vince W.  · 技术社区  · 6 年前

    我正在尝试处理一些传感器数据,对于这些数据,本地时间的分析对分析非常重要。请参阅下面的示例分析(由@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
    
    0 回复  |  直到 6 年前