简体   繁体   English

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

[英]Need to change 24:00 to 23:00 time for portion of data in Python

I have data for 2018 and 2017 that uses 24:00 hour times like this: 我有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

Whereas the rest of the data from 2016-2013 is in the 23:00 time and looks like: 而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

I'd simply like to convert the 2018-2017 data from 24:00 hours to 23:00 hours but also keep the X, Y, and Z values that are all linked to the times the same which means I need to change (for example): 我只想将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

to

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

I know this is a fairly simple fix but just attempting pd.to_datetime for the column obviously requires 23:00 hour formats. 我知道这是一个相当简单的修复程序,但是仅尝试对列使用pd.to_datetime显然需要23:00小时格式。

Is there an easy way to change these dates? 有没有简单的方法可以更改这些日期?

You can use pd.to_timedelta to help: 您可以使用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)

Output: 输出:

           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

I don't know how your data is structured, but just iterate through it and splice the string (if it's a string) to see if '24' shows up in the 5th to last chars to the 3rd to last char. 我不知道您的数据的结构,只是对其进行遍历并拼接字符串(如果是字符串),看是否在最后5个字符至第3个字符中显示“ 24”。 Then add one to the date using the datetime module. 然后使用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)

output: 输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将日期时间00:00:00更改为24:00:00 - change datetime 00:00:00 to 24:00:00 将12 hr转换为24 hr python将24:00:00显示为24:00:00而不是00:00:00 - Converting 12 hr to 24 hr python shows 24:00:00 as 24:00:00 and not 00:00:00 如何在Python中将日期时间字符串中的时间从24:00转换为00:00? - How can I convert the time in a datetime string from 24:00 to 00:00 in Python? Python 时间数据 '2008-01-24T00:00:00:000' 与格式 '%Y-%m-%d %H:%M :%S:%f' 不匹配 - Python time data '2008-01-24T00:00:00:000' does not match format '%Y-%m-%d %H:%M :%S:%f' PostgreSQL在SELECT语句中从“ 24:00:00”到“ 00:00:00”的时间转换/格式 - PostgreSQL time conversion/formatting from “24:00:00” to “00:00:00” in SELECT statement 以数字数据类型访问datetime.time(00:00-23:59)格式 - Accessing the datetime.time (00:00 - 23:59) format in a numeric data type Python-将时间格式12:00a转换为24hour - Python - Converting time format 12:00a to 24hour ValueError:时间数据'24:00'与格式'%H:%M'不匹配 - ValueError: time data '24:00' does not match format '%H:%M' 在时间= 00:00:00的熊猫数据框中将日期更改为天+ 1 - Change date to day + 1 in a pandas dataframe where time = 00:00:00 熊猫:解析 24:00 而不是 00:00 - Pandas: parsing 24:00 instead of 00:00
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM