Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python将日期范围应用于两列_Python_Date_Pandas_Range - Fatal编程技术网

Python将日期范围应用于两列

Python将日期范围应用于两列,python,date,pandas,range,Python,Date,Pandas,Range,我的数据框如下所示: df[['reported_date', 'current_date']].head() reported_date current_date 0 2016-01-15 13:58:21 2016-01-18 00:00:00 1 2016-01-14 10:51:24 2016-01-18 00:00:00 2 2016-01-15 15:17:35 2016-01-18 00:00:00 3 2016-01-17 17:07:

我的数据框如下所示:

df[['reported_date', 'current_date']].head()
    reported_date        current_date
0   2016-01-15 13:58:21  2016-01-18 00:00:00
1   2016-01-14 10:51:24  2016-01-18 00:00:00
2   2016-01-15 15:17:35  2016-01-18 00:00:00
3   2016-01-17 17:07:10  2016-01-18 00:00:00
4   2016-01-17 17:08:23  2016-01-18 00:00:00
我可以直接应用日期减法,如:

df[['reported_date', 'current_date']].head().apply(lambda x: x[1]-x[0], axis=1)
但是当我尝试应用date_range来获取两天之间的间隔时,我得到了以下错误

df[['reported_date', 'current_date']].head().apply(lambda x: pd.date_range(x[0], x[1], freq='B'), axis=1)

"ValueError: Length of values does not match length of index"
那么,将
date\u range()
应用于
datetime
的两列的正确方法是什么呢

先谢谢你


jian

pd.date\u range
不返回间隔。它返回开始和结束之间所有datetime对象的系列(
DateTimeIndex
really)。 因为这里的开始是
reported\u date
并且是可变的,而结束是
current\u date
并且是固定的,所以会得到一系列不同的长度,这显然不适合单个(新)列


之前使用的减法给出日期之间的间隔。因此没有理由使用
pd.date\u range
x[1]-x[0]
正是您想要的。

谢谢,埃弗特,我想要的是返回的DateTimeIndex的长度。date\u range生成的项目数必须等于数据帧的长度