• <i id='00iJB'><tr id='00iJB'><dt id='00iJB'><q id='00iJB'><span id='00iJB'><b id='00iJB'><form id='00iJB'><ins id='00iJB'></ins><ul id='00iJB'></ul><sub id='00iJB'></sub></form><legend id='00iJB'></legend><bdo id='00iJB'><pre id='00iJB'><center id='00iJB'></center></pre></bdo></b><th id='00iJB'></th></span></q></dt></tr></i><div id='00iJB'><tfoot id='00iJB'></tfoot><dl id='00iJB'><fieldset id='00iJB'></fieldset></dl></div>
        <legend id='00iJB'><style id='00iJB'><dir id='00iJB'><q id='00iJB'></q></dir></style></legend>
      1. <small id='00iJB'></small><noframes id='00iJB'>

          <bdo id='00iJB'></bdo><ul id='00iJB'></ul>
        <tfoot id='00iJB'></tfoot>

        无法使用 Series 内置函数对时间戳应用方法

        时间:2023-09-14
        <legend id='Ms9F4'><style id='Ms9F4'><dir id='Ms9F4'><q id='Ms9F4'></q></dir></style></legend>

            <tbody id='Ms9F4'></tbody>
            <tfoot id='Ms9F4'></tfoot>

              • <bdo id='Ms9F4'></bdo><ul id='Ms9F4'></ul>

                  <small id='Ms9F4'></small><noframes id='Ms9F4'>

                • <i id='Ms9F4'><tr id='Ms9F4'><dt id='Ms9F4'><q id='Ms9F4'><span id='Ms9F4'><b id='Ms9F4'><form id='Ms9F4'><ins id='Ms9F4'></ins><ul id='Ms9F4'></ul><sub id='Ms9F4'></sub></form><legend id='Ms9F4'></legend><bdo id='Ms9F4'><pre id='Ms9F4'><center id='Ms9F4'></center></pre></bdo></b><th id='Ms9F4'></th></span></q></dt></tr></i><div id='Ms9F4'><tfoot id='Ms9F4'></tfoot><dl id='Ms9F4'><fieldset id='Ms9F4'></fieldset></dl></div>

                • 本文介绍了无法使用 Series 内置函数对时间戳应用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  关于以下系列:

                  0    1411161507178
                  1    1411138436009
                  2    1411123732180
                  3    1411167606146
                  4    1411124780140
                  5    1411159331327
                  6    1411131745474
                  7    1411151831454
                  8    1411152487758
                  9    1411137160544
                  Name: my_series, dtype: int64
                  

                  此命令(转换为时间戳、本地化并转换为 EST)有效:

                  This command (convert to timestamp, localize and convert to EST) works:

                  pd.to_datetime(my_series, unit='ms').apply(lambda x: x.tz_localize('UTC').tz_convert('US/Eastern'))
                  

                  但是这个失败了:

                  pd.to_datetime(my_series, unit='ms').tz_localize('UTC').tz_convert('US/Eastern')
                  

                  与:

                  TypeError                                 Traceback (most recent call last)
                  <ipython-input-3-58187a4b60f8> in <module>()
                  ----> 1 lua = pd.to_datetime(df[column], unit='ms').tz_localize('UTC').tz_convert('US/Eastern')
                  
                  /Users/josh/anaconda/envs/py34/lib/python3.4/site-packages/pandas/core/generic.py in tz_localize(self, tz, axis, copy, infer_dst)
                     3492                 ax_name = self._get_axis_name(axis)
                     3493                 raise TypeError('%s is not a valid DatetimeIndex or PeriodIndex' %
                  -> 3494                                 ax_name)
                     3495             else:
                     3496                 ax = DatetimeIndex([],tz=tz)
                  
                  TypeError: index is not a valid DatetimeIndex or PeriodIndex
                  

                  这个也是如此:

                  my_series.tz_localize('UTC').tz_convert('US/Eastern')
                  

                  与:

                  ---------------------------------------------------------------------------
                  TypeError                                 Traceback (most recent call last)
                  <ipython-input-4-0a7cb1e94e1e> in <module>()
                  ----> 1 lua = df[column].tz_localize('UTC').tz_convert('US/Eastern')
                  
                  /Users/josh/anaconda/envs/py34/lib/python3.4/site-packages/pandas/core/generic.py in tz_localize(self, tz, axis, copy, infer_dst)
                     3492                 ax_name = self._get_axis_name(axis)
                     3493                 raise TypeError('%s is not a valid DatetimeIndex or PeriodIndex' %
                  -> 3494                                 ax_name)
                     3495             else:
                     3496                 ax = DatetimeIndex([],tz=tz)
                  
                  TypeError: index is not a valid DatetimeIndex or PeriodIndex
                  

                  据我了解,上面的第二种方法(第一种失败的方法)应该有效.为什么会失败?

                  As far as I understand, the second approach above (the first one that fails) should work. Why does it fail?

                  推荐答案

                  tz_localize/tz_convert 作用于对象的 INDEX,而不是值.最简单的方法是将其转换为索引,然后进行本地化和转换.如果你想要一个系列回来,你可以使用 to_series()

                  tz_localize/tz_convert act on the INDEX of the object, not on the values. Easiest to simply turn it into an index then localize and convert. If you then want a Series back you can use to_series()

                  In [47]: pd.DatetimeIndex(pd.to_datetime(s,unit='ms')).tz_localize('UTC').tz_convert('US/Eastern')
                  Out[47]: 
                  <class 'pandas.tseries.index.DatetimeIndex'>
                  [2014-09-19 17:18:27.178000-04:00, ..., 2014-09-19 10:32:40.544000-04:00]
                  Length: 10, Freq: None, Timezone: US/Eastern
                  

                  这篇关于无法使用 Series 内置函数对时间戳应用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用python中的pandas解析年月日和小时在不同列中 下一篇:从 Binance-API (Python) 计算时间戳之外的日期

                  相关文章

                  最新文章

                • <legend id='MXaEF'><style id='MXaEF'><dir id='MXaEF'><q id='MXaEF'></q></dir></style></legend>

                    <bdo id='MXaEF'></bdo><ul id='MXaEF'></ul>

                    <small id='MXaEF'></small><noframes id='MXaEF'>

                      <tfoot id='MXaEF'></tfoot>
                    1. <i id='MXaEF'><tr id='MXaEF'><dt id='MXaEF'><q id='MXaEF'><span id='MXaEF'><b id='MXaEF'><form id='MXaEF'><ins id='MXaEF'></ins><ul id='MXaEF'></ul><sub id='MXaEF'></sub></form><legend id='MXaEF'></legend><bdo id='MXaEF'><pre id='MXaEF'><center id='MXaEF'></center></pre></bdo></b><th id='MXaEF'></th></span></q></dt></tr></i><div id='MXaEF'><tfoot id='MXaEF'></tfoot><dl id='MXaEF'><fieldset id='MXaEF'></fieldset></dl></div>