[英]Creating a new dataframe by passing a series of datetimes through an existing dataframe
I have a pandas dataframe called "testset" that looks like this (note that the date column is the index and has been converted to datetime already): 我有一个名为“ testset”的熊猫数据框,它看起来像这样(请注意,date列是索引,已经转换为datetime了):
Date Adj Close
1950-01-03 00:00:00 16.66
1950-01-04 00:00:00 16.85
1950-01-05 00:00:00 16.93
1950-01-06 00:00:00 16.98
1950-01-09 00:00:00 17.08
1950-01-10 00:00:00 17.03
1950-01-11 00:00:00 17.09
1950-01-12 00:00:00 16.76
...
and I have a series of dates (again, converted to datetime) called "triggerdates" that looks like this: 并且我有一系列日期(再次转换为日期时间),称为“ triggerdates”,如下所示:
Index Trigger Date
65 1950-10-04 00:00:00
124 1951-01-02 00:00:00
165 1951-03-02 00:00:00
208 1951-05-03 00:00:00
943 1954-04-12 00:00:00
997 1954-06-29 00:00:00
1053 1954-09-17 00:00:00
1089 1954-11-09 00:00:00
I want to pass the set of dates in the second dataset through the first to create a new dataframe, so that the expected outcome looks something like this: 我想将第二个数据集中的日期集传递给第一个数据集,以创建一个新的数据框,以便预期结果如下所示:
Date Adj Close
1950-10-04 00:00:00 18.64
1951-01-02 00:00:00 19.23
1951-03-02 00:00:00 18.21
1951-05-03 00:00:00 19.21
1954-04-12 00:00:00 20.07
1954-06-29 00:00:00 17.23
1954-09-17 00:00:00 16.12
1954-11-09 00:00:00 20.44
...
When I try and pass it through as below: 当我尝试通过以下方式通过它时:
df = np.where(triggerdates, testset['Adj Close'], np.nan)
I get the following error: 我收到以下错误:
ValueError: operands could not be broadcast together with shapes (91,) (17369,) ()
As always, any help is appreciated 与往常一样,任何帮助都值得赞赏
我认为您正在寻找reindex
testset=testset.reindex(triggerdates)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.