[英]Converting datetime.date to pandas.core.series.Series in Python?
Problem Statement: (Multiple Linear regression) A digital media company (Netflix, etc.) had launched a show.问题陈述:(多元线性回归)一家数字媒体公司(Netflix 等)推出了一个节目。 Initially, the show got a good response, but then witnessed a decline in viewership.
最初,该节目获得了良好的反响,但随后收视率下降。 The company wants to figure out what went wrong.
该公司想弄清楚出了什么问题。
I want to create an extra column ie media['days'] which basically keeps a count of the total numbers of days the show is running.我想创建一个额外的列,即 media['days'] ,它基本上记录了节目运行的总天数。 Suppose the 1st day of the show is on 1st March 2017, ie 2017-03-1.
假设演出的第一天是 2017 年 3 月 1 日,即 2017-03-1。
The code I written is as follows.我写的代码如下。
media['Date'] = pd.to_datetime(media['Date'])
#deriving "days since the show started"
import datetime
d0 = date(2017, 2, 28)
d1 = media.Date #media is a dataframe variable
delta = d1 - d0
media['Day'] = delta
The error which I get is:我得到的错误是:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
in
3 d0 = date(2017, 2, 28)
4 d1 = media.Date #media is a dataframe variable
----> 5 delta = d1 - d0
6 media['Day'] = delta
c:\DEV\work\lib\site-packages\pandas\core\ops\__init__.py in wrapper(left, right)
990 # test_dt64_series_add_intlike, which the index dispatching handles
991 # specifically.
--> 992 result = dispatch_to_index_op(op, left, right, pd.DatetimeIndex)
993 return construct_result(
994 left, result, index=left.index, name=res_name, dtype=result.dtype
c:\DEV\work\lib\site-packages\pandas\core\ops\__init__.py in dispatch_to_index_op(op, left, right,
index_class)
628 left_idx = left_idx._shallow_copy(freq=None)
629 try:
--> 630 result = op(left_idx, right)
631 except NullFrequencyError:
632 # DatetimeIndex and TimedeltaIndex with freq == None raise ValueError
TypeError: unsupported operand type(s) for -: 'DatetimeIndex' and 'datetime.date'
I can see the data type is mis-matching.我可以看到数据类型不匹配。 d0 is of the type: datetime.date &
d0的类型为:datetime.date &
d1 is of the type: pandas.core.series.Series d1的类型为:pandas.core.series.Series
So can anyone help me as to how...I can convert / parse the value of d0 to be exactly same as that of d1 .那么任何人都可以帮助我如何...我可以将d0的值转换/解析为与d1的值完全相同。
It is necessary to convert the datetime.date
in order to get the interval.有必要转换
datetime.date
以获得间隔。 To do this, you have to wrap d0
in pd.to_datetime
.为此,您必须将
d0
包装在pd.to_datetime
。
ie the following should work, giving a delta in days, if you want just the integer part, you can use dt
accessor on the datetime series.即以下应该工作,以天为单位给出增量,如果你只想要整数部分,你可以在日期时间系列上使用
dt
访问器。
delta = d1 - pd.to_datetime(d0)
# or
delta = (d1 - pd.to_datetime(d0)).dt.days
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.