需要将24:00到23:00时间更改为Python中的部分数据

时间:2019-01-03 18:52:11

标签: python python-3.x pandas datetime dataframe

我有2018年和2017年的数据,使用24:00小时:

Hour Ending         X           Y       Z
12/31/2017 24:00    11452.16    1834.87 2856.94
12/31/2017 23:00    11579.85    1855.94 2898.57
12/31/2017 22:00    11754.25    1890.36 2942.23
12/31/2017 21:00    11883.11    1907.59 2970.85
12/31/2017 20:00    12015.66    1910.72 2989.82
12/31/2017 19:00    12061.55    1923.56 3002.77
12/31/2017 18:00    11663.43    1891.09 2915.28
12/31/2017 17:00    11008.23    1803.92 2871.94
12/31/2017 16:00    10904.93    1730.49 2864.33
12/31/2017 15:00    11014.92    1673.37 2862.77
12/31/2017 14:00    11099.28    1604.28 2853.98
12/31/2017 13:00    11088.55    1585.55 2841.05
12/31/2017 12:00    10989.86    1578.52 2822.75
12/31/2017 11:00    10849.49    1578.38 2802.3
12/31/2017 10:00    10600.86    1581.44 2774.18
12/31/2017 09:00    10184.76    1532.89 2715.56
12/31/2017 08:00    9826.52     1461.63 2672.01
12/31/2017 07:00    9556.41     1399.1  2611.86
12/31/2017 06:00    9260.16     1341.11 2578.8
12/31/2017 05:00    9113.75     1328.5  2581.56
12/31/2017 04:00    9025.76     1346.87 2582.43
12/31/2017 03:00    9044.65     1343.63 2584.13
12/31/2017 02:00    9194.51     1358.57 2600.79
12/31/2017 01:00    9444.48     1379.35 2621.97
12/30/2017 24:00    9794.9      1426.91 2679.92

而2016-2013年的其余数据是在23:00时间,如下所示:

Hour Ending         X           Y       Z
01/04/2013 0:00 9166.3577   1377.48441  1646.06411
01/03/2013 23:00    9700.616845999999   1454.42221  1684.4460960000001
01/03/2013 22:00    10236.5831  1518.723561 1747.198325
01/03/2013 21:00    10622.79608 1550.834297 1787.9794949999998
01/03/2013 20:00    10845.332390000001  1562.49244  1793.3864859999999
01/03/2013 19:00    10948.436590000001  1578.140944 1797.555973
01/03/2013 18:00    10601.329259999999  1515.094035 1752.964543
01/03/2013 17:00    10043.38204 1434.163607 1725.7288989999997
01/03/2013 16:00    9927.267078 1394.351864 1708.8579579999998
01/03/2013 15:00    9882.591065999999   1365.690266 1711.5373809999999
01/03/2013 14:00    10047.71838 1406.555055 1723.633549
01/03/2013 13:00    10290.2543  1445.03692  1717.541841
01/03/2013 12:00    10585.86702 1519.160058 1739.736317
01/03/2013 11:00    10895.58775 1617.807443 1742.36163
01/03/2013 10:00    10918.89208 1686.252509 1730.4538109999999
01/03/2013 9:00     10929.63421 1747.157416 1769.523491
01/03/2013 8:00     10944.18396 1749.1523829999999  1722.127739
01/03/2013 7:00     10624.5241  1690.290167 1680.816472
01/03/2013 6:00     9924.627883 1570.496102 1621.679814
01/03/2013 5:00     9370.169947 1475.116882 1580.4176400000001
01/03/2013 4:00     9170.732963 1424.242721 1576.282469
01/03/2013 3:00     9142.024671 1401.497276 1564.750211
01/03/2013 2:00     9240.30997  1438.449286 1565.629803
01/03/2013 1:00     9455.203629000001   1445.505592 1580.642498
01/03/2013 0:00     9822.1846   1428.1571800000002  1582.02934

我只想将2018-2017数据从24:00小时转换为23:00小时,但还要保持与时间相关联的X,Y和Z值不变,这意味着我需要更改(例如):

Hour Ending         X           Y       Z
12/31/2017 24:00    11452.16    1834.87 2856.94

Hour Ending         X           Y       Z
01/01/2018 00:00    11452.16    1834.87 2856.94

我知道这是一个非常简单的修复程序,但是仅尝试pd.to_datetime访问该列显然需要23:00小时格式。

有没有简单的方法可以更改这些日期?

2 个答案:

答案 0 :(得分:3)

您可以使用pd.to_timedelta帮助:

s = df['Hour Ending'].str.split()
df['Hour Ending'] = pd.to_datetime(s.str[0]) + pd.to_timedelta(s.str[1].str.split(':').str[0] +' hours')
print(df)

输出:

           Hour Ending         X        Y        Z
0  2018-01-01 00:00:00  11452.16  1834.87  2856.94
1  2017-12-31 23:00:00  11579.85  1855.94  2898.57
2  2017-12-31 22:00:00  11754.25  1890.36  2942.23
3  2017-12-31 21:00:00  11883.11  1907.59  2970.85
4  2017-12-31 20:00:00  12015.66  1910.72  2989.82
5  2017-12-31 19:00:00  12061.55  1923.56  3002.77
6  2017-12-31 18:00:00  11663.43  1891.09  2915.28
7  2017-12-31 17:00:00  11008.23  1803.92  2871.94
8  2017-12-31 16:00:00  10904.93  1730.49  2864.33
9  2017-12-31 15:00:00  11014.92  1673.37  2862.77
10 2017-12-31 14:00:00  11099.28  1604.28  2853.98
11 2017-12-31 13:00:00  11088.55  1585.55  2841.05
12 2017-12-31 12:00:00  10989.86  1578.52  2822.75
13 2017-12-31 11:00:00  10849.49  1578.38  2802.30
14 2017-12-31 10:00:00  10600.86  1581.44  2774.18
15 2017-12-31 09:00:00  10184.76  1532.89  2715.56
16 2017-12-31 08:00:00   9826.52  1461.63  2672.01
17 2017-12-31 07:00:00   9556.41  1399.10  2611.86
18 2017-12-31 06:00:00   9260.16  1341.11  2578.80
19 2017-12-31 05:00:00   9113.75  1328.50  2581.56
20 2017-12-31 04:00:00   9025.76  1346.87  2582.43
21 2017-12-31 03:00:00   9044.65  1343.63  2584.13
22 2017-12-31 02:00:00   9194.51  1358.57  2600.79
23 2017-12-31 01:00:00   9444.48  1379.35  2621.97
24 2017-12-31 00:00:00   9794.90  1426.91  2679.92

答案 1 :(得分:0)

我不知道您的数据的结构,只是对其进行遍历并拼接字符串(如果是字符串)以查看“ 24”是否出现在倒数第5位至第3位。然后使用datetime模块在日期中添加一个。

import datetime

lines = ['12/31/2017 24:00', '12/31/2017 23:00', '12/30/2017 24:00']
for line in lines:
    print('PREVIOUS : ' + line)
    # Convert a 24 hour to 0 hour
    if line[-5:-3] == '24':
        (date, time) = line.split()
        time = time[:-5] + '0' + time[-3:]
        date = datetime.datetime.strptime(date, '%m/%d/%Y') + datetime.timedelta(days=1)
        print('CHANGED  : ' + date.strftime('%m/%d/%Y') + ' ' + time)
    else:
        print('UNCHANGED: ' + line)

输出:

PREVIOUS : 12/31/2017 24:00
AFTER    : 01/01/2018 0:00
PREVIOUS : 12/31/2017 23:00
UNCHANGED: 12/31/2017 23:00
PREVIOUS : 12/30/2017 24:00
AFTER    : 12/31/2017 0:00