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

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 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口