首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用小时来抵消数据的一部分

用小时来抵消数据的一部分
EN

Stack Overflow用户
提问于 2015-03-24 11:48:59
回答 1查看 52关注 0票数 1

我有一部分测量仪器的时间被配置错了,这需要纠正。

因此,我试图在2小时内抵消或移动部分数据,但我似乎无法让它使用我的以下代码:

代码语言:javascript
复制
dfs['2015-03-23 10:45:00':'2015-03-23 13:15:00'].shift(freq=datetime.timedelta(hours=2))

不过,我不知道这样做是否容易。

希望有人理解我的问题:)

代码语言:javascript
复制
>>> dfs.info()
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 11979 entries, 2015-03-23 10:45:05 to 2015-03-23 16:19:32
Data columns (total 11 columns):
CH-1[V]        11979 non-null float64
CH-2[V]        11979 non-null float64
CH-3[V]        11979 non-null float64
CH-4[V]        11979 non-null float64
CH-5[V]        11979 non-null float64
CH-6[V]        11979 non-null float64
CH-7[V]        11979 non-null float64
CH-9[C]        11979 non-null float64
CH-10[C]       11979 non-null float64
Event          11979 non-null int64
Unnamed: 11    0 non-null float64
dtypes: float64(10), int64(1)
memory usage: 1.1 MB
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-24 12:40:09

熊猫指数是不可变的,所以我们不能改变指数。但是,我们可以将索引设置为DataFrame列,修改该列,然后重置索引:

代码语言:javascript
复制
import numpy as np
import pandas as pd

# Suppose this is your `dfs`:
index = pd.date_range('2015-03-23 10:45:05', '2015-03-23 16:19:32', freq='T')
N = len(index)
dfs = pd.DataFrame(np.arange(N), index=index)

# move the index into a column
dfs = dfs.reset_index()
mask = (index >= '2015-03-23 10:45:00') & (index <= '2015-03-23 13:15:00')

# shift the masked values in the column
dfs.loc[mask, 'index'] += pd.Timedelta(hours=2)

# use the index column as the index
dfs = dfs.set_index(['index'])

这表明索引已经移动了2个小时:

代码语言:javascript
复制
In [124]: dfs.iloc[np.where(mask)[0].max()-1:].head(5)
Out[124]: 
                       0
index                   
2015-03-23 15:13:05  148
2015-03-23 15:14:05  149    <-- shifted by 2 hours
2015-03-23 13:15:05  150    <-- unchanged
2015-03-23 13:16:05  151
2015-03-23 13:17:05  152
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29231909

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档