【问题标题】:How to find the first of week from a series of dtype('<M8[ns]')如何从一系列 dtype('<M8[ns]') 中找到第一周
【发布时间】:2017-11-30 16:22:58
【问题描述】:

我有一系列p 类型的时间戳:dtype('&lt;M8[ns]')

我正在尝试将其转换为一周的第一天,如下所示:

p - pd.Timedelta(days=p.dt.dayofweek) 

这显然不是正确的答案

TypeError: Invalid type <class 'pandas.core.series.Series'>. Must be int or float.

但是什么是?

------------完整堆栈跟踪 -----

TypeErrorTraceback (most recent call last)
<ipython-input-699-bb55f007b4d9> in <module>()
      2 p= testDf.PlayDate.drop_duplicates()
      3 #p=pd.to_datetime(p)
----> 4 p - pd.to_timedelta(p.dt.dayofweek, unit='D')
      5 

C:\DS\Installs\Anaconda3\lib\site-packages\pandas\core\ops.py in wrapper(left, right, name, na_op)
    607 
    608         time_converted = _TimeOp.maybe_convert_for_time_op(left, right, name,
--> 609                                                            na_op)
    610 
    611         if time_converted is None:

C:\DS\Installs\Anaconda3\lib\site-packages\pandas\core\ops.py in maybe_convert_for_time_op(cls, left, right, name, na_op)
    567             return None
    568 
--> 569         return cls(left, right, name, na_op)
    570 
    571 

C:\DS\Installs\Anaconda3\lib\site-packages\pandas\core\ops.py in __init__(self, left, right, name, na_op)
    280             left, right = left.align(right, copy=False)
    281 
--> 282         lvalues = self._convert_to_array(left, name=name)
    283         rvalues = self._convert_to_array(right, name=name, other=lvalues)
    284 

C:\DS\Installs\Anaconda3\lib\site-packages\pandas\core\ops.py in _convert_to_array(self, values, name, other)
    396             supplied_dtype = values.dtype
    397         inferred_type = supplied_dtype or lib.infer_dtype(values)
--> 398         if (inferred_type in ('datetime64', 'datetime', 'date', 'time') or
    399                 com.is_datetimetz(inferred_type)):
    400             # if we have a other of timedelta, but use pd.NaT here we

TypeError: data type "datetime" not understood

【问题讨论】:

  • 能否添加重现错误的数据?

标签: python-3.x pandas numpy


【解决方案1】:

您当前的代码试图从一个系列中创建一个 timedelta 对象,因此构造函数失败。

p - pd.to_timedelta(p.dt.dayofweek, unit='D')

应该做你想做的。

编辑:

例如:

help(pd)
Help on package pandas:

...

VERSION
    0.18.1

...



p = pd.date_range('1/1/2017', '1/23/2017').to_series().reset_index(drop=True)

p.dtype
Out[8]: dtype('<M8[ns]')

print(p.dtype)
datetime64[ns]

p - pd.to_timedelta(p.dt.dayofweek, unit='D')
Out[10]: 
0    2016-12-26
1    2017-01-02
2    2017-01-02
3    2017-01-02
4    2017-01-02
5    2017-01-02
6    2017-01-02
7    2017-01-02
8    2017-01-09
9    2017-01-09
10   2017-01-09
11   2017-01-09
12   2017-01-09
13   2017-01-09
14   2017-01-09
15   2017-01-16
16   2017-01-16
17   2017-01-16
18   2017-01-16
19   2017-01-16
20   2017-01-16
21   2017-01-16
22   2017-01-23
dtype: datetime64[ns]

print(pd.to_timedelta(p.dt.dayofweek, unit='D').dtype)
timedelta64[ns]

编辑2:

总结下面的评论讨论,仍然遇到的问题是由于 pandas 版本过时造成的。对于大于0.18.0 的 pandas 版本,上面的解决方案应该可以工作。

【讨论】:

  • 谢谢,这给出了错误:TypeError: data type "datetime" not understood 我做了一个print(p.dtype),它给出了:datetime64[ns],而不是仅仅输入p.type,它给出了:dtype('&lt;M8[ns]')。正在进一步调查,看看我是否能弄清楚......
  • @user1761806 print(p.dtype)p.dtype 之间的区别不应该是相关的;我也一样。这是因为__str____repr__ 不同:p.dtype.__str__() Out[241]: 'datetime64[ns]' p.dtype.__repr__() Out[242]: "dtype('&lt;M8[ns]')",而两者的区别只是一般数据类型和特定数据类型之间的区别,这里更多:stackoverflow.com/questions/29206612/…
  • @user1761806 您能否编辑您的问题以包含当前错误的完整堆栈跟踪?
  • 完成.. 我试图自己破译它,但遗憾的是失败了。
  • 好吧,这可能是一个 python 错误吗?失败的行是:` if (inferred_type in ('datetime64', 'datetime', 'date', 'time') or` 失败的原因是因为inferred_type = datetime64[ns]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-23
相关资源
最近更新 更多