Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.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 如何在pandas中对函数进行矢量化,以使用periodIndex作为开始:停止索引以使用.loc分割另一个数据帧?_Python_Pandas_Timestamp_Vectorization - Fatal编程技术网

Python 如何在pandas中对函数进行矢量化,以使用periodIndex作为开始:停止索引以使用.loc分割另一个数据帧?

Python 如何在pandas中对函数进行矢量化,以使用periodIndex作为开始:停止索引以使用.loc分割另一个数据帧?,python,pandas,timestamp,vectorization,Python,Pandas,Timestamp,Vectorization,我似乎不知道如何让这个函数工作。它设计用于从df中的periodIndex提取开始时间和结束时间属性,将其分配给df['first\u date']和df['last\u date'],然后使用这些值作为开始和停止索引,从第二个数据帧中选择行: def stats_wk(self, df, ph_df): df['first_date'] = df.index.start_time df['last_date'] = df.index.end_time

我似乎不知道如何让这个函数工作。它设计用于从df中的periodIndex提取开始时间和结束时间属性,将其分配给
df['first\u date']
df['last\u date']
,然后使用这些值作为开始和停止索引,从第二个数据帧中选择行:

def stats_wk(self, df, ph_df):
        df['first_date'] = df.index.start_time
        df['last_date'] = df.index.end_time

        ph_df_slice = ph_df.loc[df['first_date'] : df['last_date']]
当我运行此代码时,我得到:

Traceback (most recent call last):
  File "C:\Users\s\PycharmProjects\project\lib\site-packages\pandas\core\indexes\datetimes.py", line 718, in slice_indexer
    return Index.slice_indexer(self, start, end, step, kind=kind)
  File "C:\Users\s\PycharmProjects\project\lib\site-packages\pandas\core\indexes\base.py", line 4960, in slice_indexer
    start_slice, end_slice = self.slice_locs(start, end, step=step, kind=kind)
  File "C:\Users\s\PycharmProjects\project\lib\site-packages\pandas\core\indexes\base.py", line 5161, in slice_locs
    start_slice = self.get_slice_bound(start, "left", kind)
  File "C:\Users\s\PycharmProjects\project\lib\site-packages\pandas\core\indexes\base.py", line 5077, in get_slice_bound
    slc = self.get_loc(label)
  File "C:\Users\s\PycharmProjects\project\lib\site-packages\pandas\core\indexes\datetimes.py", line 586, in get_loc
    raise InvalidIndexError(key)
pandas.errors.InvalidIndexError: date
2015-10-05/2015-10-11   2015-10-05
2015-10-12/2015-10-18   2015-10-12
2015-10-19/2015-10-25   2015-10-19

Freq: W-SUN, Name: first_date, dtype: datetime64[ns]

Process finished with exit code -1
在我看来,它可能会将整个索引传递到
.loc
,而不是相应行中的单个值?但是如果是这样的话,当
df['first_date']
应该是从
df.index.start_time
派生的时间戳对象时,为什么或者如何传递整个索引?甚至可以将此函数矢量化吗

如果能得到任何帮助,我将不胜感激