Galen-N 2021-04-14 23:05 采纳率: 50%
浏览 57
已结题

Python中时间戳转化问题

data = data.groupby([pd.Grouper(key='Timestamp', freq='D')]).first().reset_index()

 

报错提示如下:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-34-bb950f0a9cc9> in <module>
----> 1 data = data.groupby([pd.Grouper(key='Timestamp', freq='D')]).first().reset_index()

~\Anaconda3\lib\site-packages\pandas\core\frame.py in groupby(self, by, axis, level, as_index, sort, group_keys, squeeze, observed, dropna)
   6725             squeeze=squeeze,
   6726             observed=observed,
-> 6727             dropna=dropna,
   6728         )
   6729 

~\Anaconda3\lib\site-packages\pandas\core\groupby\groupby.py in __init__(self, obj, keys, axis, level, grouper, exclusions, selection, as_index, sort, group_keys, squeeze, observed, mutated, dropna)
    566                 observed=observed,
    567                 mutated=self.mutated,
--> 568                 dropna=self.dropna,
    569             )
    570 

~\Anaconda3\lib\site-packages\pandas\core\groupby\grouper.py in get_grouper(obj, key, axis, level, sort, observed, mutated, validate, dropna)
    837                 dropna=dropna,
    838             )
--> 839             if not isinstance(gpr, Grouping)
    840             else gpr
    841         )

~\Anaconda3\lib\site-packages\pandas\core\groupby\grouper.py in __init__(self, index, grouper, obj, name, level, sort, observed, in_axis, dropna)
    492             # check again as we have by this point converted these
    493             # to an actual value (rather than a pd.Grouper)
--> 494             _, grouper, _ = self.grouper._get_grouper(self.obj, validate=False)
    495             if self.name is None:
    496                 self.name = grouper.result_index.name

~\Anaconda3\lib\site-packages\pandas\core\resample.py in _get_grouper(self, obj, validate)
   1483         # create the resampler and return our binner
   1484         r = self._get_resampler(obj)
-> 1485         r._set_binner()
   1486         return r.binner, r.grouper, r.obj
   1487 

~\Anaconda3\lib\site-packages\pandas\core\resample.py in _set_binner(self)
    193         """
    194         if self.binner is None:
--> 195             self.binner, self.grouper = self._get_binner()
    196 
    197     def _get_binner(self):

~\Anaconda3\lib\site-packages\pandas\core\resample.py in _get_binner(self)
    200         has already been called.
    201         """
--> 202         binner, bins, binlabels = self._get_binner_for_time()
    203         assert len(bins) == len(binlabels)
    204         bin_grouper = BinGrouper(bins, binlabels, indexer=self.groupby.indexer)

~\Anaconda3\lib\site-packages\pandas\core\resample.py in _get_binner_for_time(self)
   1040         if self.kind == "period":
   1041             return self.groupby._get_time_period_bins(self.ax)
-> 1042         return self.groupby._get_time_bins(self.ax)
   1043 
   1044     def _downsample(self, how, **kwargs):

~\Anaconda3\lib\site-packages\pandas\core\resample.py in _get_time_bins(self, ax)
   1503             closed=self.closed,
   1504             origin=self.origin,
-> 1505             offset=self.offset,
   1506         )
   1507         # GH #12037

~\Anaconda3\lib\site-packages\pandas\core\resample.py in _get_timestamp_range_edges(first, last, freq, closed, origin, offset)
   1744     """
   1745     if isinstance(freq, Tick):
-> 1746         index_tz = first.tz
   1747         if isinstance(origin, Timestamp) and (origin.tz is None) != (index_tz is None):
   1748             raise ValueError("The origin must have the same timezone as the index.")

AttributeError: 'NaTType' object has no attribute 'tz'
  • 写回答

1条回答 默认 最新

  • 关注

    猜你的数据索引是日期数据吧, 想按天筛选的第1条数据, 可以试试:

    data.groupby(datar.to_period('D').index).first().reset_index()
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 4月6日
  • 已采纳回答 3月29日

悬赏问题

  • ¥15 求一下解题思路,完全不懂
  • ¥15 tensorflow
  • ¥15 densenet网络结构中,特征以cat方式复用后是怎么进行误差回传的
  • ¥15 STM32G471芯片spi设置了8位,总是发送16位
  • ¥15 R语言并行计算beta-NTI中tree文件的类型
  • ¥15 如何解读marsbar导出的ROI数据?
  • ¥20 求友友协助弄一下基于STC89C52单片机的声光控制灯原理图
  • ¥15 arduino双向交通灯设计
  • ¥15 有没有会粒子群算法的大能(○゜ε^○)求带不会出收敛图😭
  • ¥15 Matlab读取根元素出错