Pandas 将日期时间列向后移动一小时

Pandas 将日期时间列向后移动一小时,pandas,date,shift,Pandas,Date,Shift,我在DF(df1)中有如下开始和结束的数据,我试图移动下面的“0”和“1”列,以便将日期和时间向后移动一小时,以便日期和时间从小时==0开始,而不是从小时==1开始 数据启动(df1)- 数据端(df1)- 我需要将开始的日期和时间向后移一小时,因此开始时间是小时开始,而不是小时结束- 0 1 2 3 4 5 6 7 0 20160101 000 7.977169 109404

我在DF(df1)中有如下开始和结束的数据,我试图移动下面的“0”和“1”列,以便将日期和时间向后移动一小时,以便日期和时间从小时==0开始,而不是从小时==1开始

数据启动(df1)-

数据端(df1)-

我需要将开始的日期和时间向后移一小时,因此开始时间是小时开始,而不是小时结束-

          0    1         2         3         4    5         6       7  
0  20160101  000  7.977169  109404.0  20160101  100  4.028678   814.0   
1  20160101  100  8.420204  128546.0  20160101  200  4.673662  2152.0   
2  20160101  200  9.515370  165931.0  20160101  300  8.019863  8100.0    
以下面的日期和时间结束-

             0     1         2        3         4     5         6      7  
8780  20161231  2000  4.198906  11371.0  20161231  2100  0.995571  131.0   
8781  20161231  2100  4.787433  19083.0  20161231  2200  1.029809    NaN   
8782  20161231  2200  3.987506   9354.0  20161231  2300  0.900942    NaN   
8783  20161231  2300  3.284947   1815.0  20170101     0  0.899262    NaN  
而且,我真的不知道如何实现这一点,也不知道如何对其进行研究。谢谢,

使用,移动列
0
1
,然后在
df2
的列
0
上使用来填充缺少的值,然后在df2的列1上使用
。fillna
来填充
NaN
值,最后使用将数据帧
df2
与数据帧
df1
连接:

df2 = df1[['0', '1']].shift()
df2['0'] = df2['0'].bfill()
df2['1'] = df2['1'].fillna('000')
df2 = df2.join(df1.loc[:, '2':])


您可以在pandas中执行减法(考虑到数据帧中的数据不是字符串类型)

我将向你展示一个如何做到这一点的例子

import pandas as pd 

df = pd.DataFrame()

df['time'] = [0,100,500,2100,2300,0]    #creating dataframe 

df['time'] = df['time']-100             #This is what you want to do
现在你的数据将被减去100

有一种情况,当减去0时,你会得到-100作为时间。为此,您可以这样做:

for i in range(len(df['time'])):
    if df['time'].iloc[i]== -100:
        df['time'].iloc[i]=2300

最好创建一个合适的datetime对象,然后简单地将小时数作为一个总数删除,这样可以在几天内处理任何修订。然后,我们可以使用
dt.strftime
重新创建对象(字符串)列

# print(df2)
             0     1         2         3         4     5         6       7
0     20160101   000  7.977169  109404.0  20160101   100  4.028678   814.0
1     20160101   100  8.420204  128546.0  20160101   200  4.673662  2152.0
2     20160101   200  9.515370  165931.0  20160101   300  8.019863  8100.0
...
8780  20160101   300  4.198906   11371.0  20161231  2100  0.995571   131.0
8781  20161231  2100  4.787433   19083.0  20161231  2200  1.029809     NaN
8782  20161231  2200  3.987506    9354.0  20161231  2300  0.900942     NaN
8783  20161231  2300  3.284947    1815.0  20170101     0  0.899262     NaN
import pandas as pd 

df = pd.DataFrame()

df['time'] = [0,100,500,2100,2300,0]    #creating dataframe 

df['time'] = df['time']-100             #This is what you want to do
for i in range(len(df['time'])):
    if df['time'].iloc[i]== -100:
        df['time'].iloc[i]=2300
s = pd.to_datetime(
    df[0].astype(str) + df[1].astype(str).str.zfill(4), format="%Y%m%d%H%M"
)

0      2016-01-01 01:00:00
1      2016-01-01 02:00:00
2      2016-01-01 03:00:00
8780   2016-12-31 21:00:00
8781   2016-12-31 22:00:00
8782   2016-12-31 23:00:00
8783   2017-01-01 00:00:00
dtype: datetime64[ns]

df[1] = (s - pd.DateOffset(hours=1)).dt.strftime("%H%M").str.lstrip("0").str.zfill(3)
df[0] = (s - pd.DateOffset(hours=1)).dt.strftime("%Y%d%m")

print(df)

             0     1         2         3         4     5         6       7
0     20160101   000  7.977169  109404.0  20160101   100  4.028678   814.0
1     20160101   100  8.420204  128546.0  20160101   200  4.673662  2152.0
2     20160101   200  9.515370  165931.0  20160101   300  8.019863  8100.0
8780  20163112  2000  4.198906   11371.0  20161231  2100  0.995571   131.0
8781  20163112  2100  4.787433   19083.0  20161231  2200  1.029809     NaN
8782  20163112  2200  3.987506    9354.0  20161231  2300  0.900942     NaN
8783  20163112  2300  3.284947    1815.0  20170101     0  0.899262     NaN