Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.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_Pandas - Fatal编程技术网

Python 两个日期列,用于生成具有选定日期的新系列

Python 两个日期列,用于生成具有选定日期的新系列,python,pandas,Python,Pandas,我有一个带有两个日期列的数据框,每一行对应一个不相交的时间间隔。我试图生成一个系列,其中包含原始列中从最小日期到最大日期的所有日期作为索引,如果它是原始时间间隔之一内的日期,则其值为1 pd.DataFrame({"A":[pd.Timestamp("2017-1-1"), pd.Timestamp("2017-2-1")], "B": [pd.Timestamp("2017-1-3"), pd.Timestamp("2017-2-3")]}) id A

我有一个带有两个日期列的数据框,每一行对应一个不相交的时间间隔。我试图生成一个系列,其中包含原始列中从最小日期到最大日期的所有日期作为索引,如果它是原始时间间隔之一内的日期,则其值为1


pd.DataFrame({"A":[pd.Timestamp("2017-1-1"), pd.Timestamp("2017-2-1")], 
             "B": [pd.Timestamp("2017-1-3"), pd.Timestamp("2017-2-3")]})

id  A           B
0   2017-01-01  2017-01-03
1   2017-02-01  2017-02-03
对此,


pd.DataFrame({"A":[pd.Timestamp("2017-1-1"),pd.Timestamp("2017-1-2"),pd.Timestamp("2017-1-3"),
                   pd.Timestamp("2017-2-1"),pd.Timestamp("2017-2-2"),pd.Timestamp("2017-2-3")], 
             "B": [1,1,1,1,1,1]})

id  A           B
0   2017-01-01  1
1   2017-01-02  1
2   2017-01-03  1
3   2017-02-01  1
4   2017-02-02  1
5   2017-02-03  1


不是真正的蟒蛇,但我认为它解决了你的问题:

在[1]中: 从日期时间导入日期,时间增量 作为pd进口熊猫 df=pd.DataFrame{A:[pd.Timestamp2017-1-1,pd.Timestamp2017-2-1], B:[pd.Timestamp2017-1-3,pd.Timestamp2017-2-3]} 日期列表=[] 对于rangelendf中的k: sdate=df.iloc[k,0]开始日期 edate=df.iloc[k,1]结束日期 delta=edate-作为时间delta的sdate 对于范围为delta.days+1的i: day=sdate+timedeltadays=i 日期_list.appendday final=pd.DataFramedata=dates\u列表,列=['A'] 最终['B']=1 最终的 出[1]: A B 0 2017-01-01 1 1 2017-01-02 1 2 2017-01-03 1 3 2017-02-01 1 4 2017-02-02 1 5 2017-02-03 1
你有没有关于你的数据的例子,或者你到目前为止已经尝试过的东西?我可以提供一个例子,但我觉得它是非常不言自明的。我已经尝试了许多使用循环的方法,但我觉得似乎有一种聪明的方法可以使用Pandas内置函数来绕过它。如果您提供代码来快速复制此dataframeHope,我们自己尝试会容易得多。希望这会有所帮助,但不确定如何显示输出