Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.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 多索引移位中的DatetimeIndex_Python_Pandas_Dataframe_Multi Index_Datetimeindex - Fatal编程技术网

Python 多索引移位中的DatetimeIndex

Python 多索引移位中的DatetimeIndex,python,pandas,dataframe,multi-index,datetimeindex,Python,Pandas,Dataframe,Multi Index,Datetimeindex,我有一些Pandas(python)数据帧,它们是通过大约每8毫秒收集一次数据创建的。数据被分解成块,序列在块中重新启动。所有块都有一个标签,并且有一个timestamp列,指示从文件开始收集样本的时间。为了获得一个想法,框架如下所示: | | EXPINDEX | EXPTIMESTAMP | DATA1 | DATA2 | ----------------------------------------------------- | BLOCK | 0 |

我有一些Pandas(python)数据帧,它们是通过大约每8毫秒收集一次数据创建的。数据被分解成块,序列在块中重新启动。所有块都有一个标签,并且有一个timestamp列,指示从文件开始收集样本的时间。为了获得一个想法,框架如下所示:

|        | EXPINDEX | EXPTIMESTAMP | DATA1 | DATA2 |
-----------------------------------------------------
| BLOCK  | 0        |              |       |       |
| Block1 | 1        | 0            | .423  | .926  |
|        | 2        | 8.215        | .462  | .919  |
|        | 3        | 17.003       | .472  | .904  |
| Block2 | 4        | 55.821       | .243  | .720  |
|        | 5        | 63.521       | .237  | .794  |
| ...    | ...      | ...          | ...   | ...   |
------------------------------------------------------
EXPTIMESTAMP列是一个DateTimeIndex。我想做的是保留该列以供以后使用,但使用块相对DateTimeIndex创建不同的子索引,例如:

|        |                | EXPTIMESTAMP | DATA1 | DATA2 |
----------------------------------------------------------
| BLOCK  | BLOCKTIMESTAMP |              |       |       |
| Block1 | 0              | 0            | .423  | .926  |
|        | 8.215          | 8.215        | .462  | .919  |
|        | 17.003         | 17.003       | .472  | .904  |
| Block2 | 0              | 55.821       | .243  | .720  |
|        | 7.700          | 63.521       | .237  | .794  |
| ...    | ...            | ...          | ...   | ...   |
----------------------------------------------------------
我已经让它工作了:

blockreltimestamp = []
blocks = list(df.index.levels[0])
for block in blocks:
   dfblock = df.xs(block, level='BLOCK').copy()
   dfblock["InitialVal"] = dfblock.iloc[0]["EXPTIMESTAMP"]
   reltime = dfsblock["EXPTIMESTAMP"] - dfblock["InitialVal"]
   blockreltimestamp.extend(list(reltime))
df["BLOCKTIMESTAMP"] = blockreltimestamp
df.set_index(["BLOCK","BLOCKTIMESTAMP"], drop=False, inplace=True)
但我想知道是否有一种更干净/更高效/更像熊猫的方式来进行这种类型的转换


谢谢

更干净的解决方案最终在非多索引数据帧上工作,其中BLOCK仍然是一个带有块ID的列,EXPTIMESTAMP是一个列,正如我最终希望的那样。在此基础上,我使用了pandas的groupby功能:

initialvalmatrix = df.groupby("BLOCK").min()[["EXPTIMESTAMP"]]
这将创建一个索引为“BLOCK”的数据帧,以及包含每个块的最小值“EXPTIMESTAMP”的列“EXPTIMESTAMP”

为了清楚起见,我将“EXPTIMESTAMP”列重命名为“INITIALBYBLOCK”:

然后,我使用pandas的apply跨列运行函数,以计算“BLOCKTIMESTAMP”列:

…其中“apply_zero_timestamp”函数定义为:

def apply_zero_timestamp(series, tslookup):
    zeroval = series["EXPTIMESTAMP"] - tslookup["INITIALBYBLOCK"][series["BLOCK"]]
    return zeroval
最后,我只需按照我的要求设置索引:

df.set_index(["BLOCK","BLOCKTIMESTAMP"], drop=False, inplace=True)
希望有帮助

def apply_zero_timestamp(series, tslookup):
    zeroval = series["EXPTIMESTAMP"] - tslookup["INITIALBYBLOCK"][series["BLOCK"]]
    return zeroval
df.set_index(["BLOCK","BLOCKTIMESTAMP"], drop=False, inplace=True)