在具备条件的情况下,将日期时间增加一秒。

huangapple go评论69阅读模式
英文:

Add one second to datetime with condition

问题

看到下面的示例熊猫数据。

ID	DT	                InOut
120	2022-12-22-02:12:123	IN
120	2022-12-23-04:12:456	OUT
120	2022-12-26-08:11:125	IN
120	2022-12-30-02:12:126	OUT

首先,我需要将我的日期时间变量更改为仅保留第二部分的两位数(例如:2023-03-03 14:11:43)。然后,我需要仅对InOut等于OUT的日期时间变量添加一秒。

我的dt变量处于datetime64[ns]格式。我无法为日期时间变量创建虚拟数据。

感谢任何帮助!

import pandas as pd
df = pd.DataFrame({'ID': [120, 120, 120, 120], 
                   'InOut': ['IN', 'OUT', 'IN', 'OUT']}) 
英文:

See the sample panda data below.

ID	DT	                InOut
120	2022-12-22-02:12:123	IN
120	2022-12-23-04:12:456	OUT
120	2022-12-26-08:11:125	IN
120	2022-12-30-02:12:126	OUT

First I need to change my datetime variable to only 2 digits for the second part (ex: 2023-03-03 14:11:43).Then I need to add one second to my datetime variable only for InOut = OUT.

My dt variable is the in datetime64[ns] format.
I wasn't able to create dummy data for the datetime variable.

Thanks for any help!

import pandas as pd
df = pd.DataFrame({'ID': [120, 120, 120, 120], 
                   'InOut': ['IN', 'OUT', 'IN', 'OUT']}) 

答案1

得分: 0

你可以使用strftime()函数将你的日期时间变量转换为所需的格式。要将日期时间变量转换为所需的格式,可以执行以下操作:

df['DT'] = df['DT'].apply(lambda x: datetime.strftime(x, '%Y-%m-%d %H:%M:%S'))

如果你想在InOut=Out的情况下将日期时间增加一秒:

df.loc[df['InOut'] == 'OUT', 'DT'] = df.loc[df['InOut'] == 'OUT', 'DT'].apply(lambda x: datetime.strptime(x, '%Y-%m-d %H:%M:%S') + timedelta(seconds=1))
英文:

You can convert your datetime variable to the desired format using the strftime(). To convert datetime variable to desired format you can do:

df['DT'] = df['DT'].apply(lambda x: datetime.strftime(x, '%Y-%m-%d %H:%M:%S'))

and if you want to add a second to datetimes with InOut=Out:

df.loc[df['InOut'] == 'OUT', 'DT'] = df.loc[df['InOut'] == 'OUT', 'DT'].apply(lambda x: datetime.strptime(x, '%Y-%m-%d %H:%M:%S') + timedelta(seconds=1))

答案2

得分: 0

将DT列转换为日期时间格式,并将秒数截断为两位小数,然后使用.loc根据条件添加一秒。

df['DT'] = (pd.to_datetime(df['DT'], format='%Y-%m-%d-%H:%M:%S%f').dt.floor('s')
              .apply(lambda x: x.replace(microsecond=(x.microsecond // 1000) * 1000)))
df.loc[df['InOut'] == 'OUT', 'DT'] += pd.Timedelta(seconds=1)

    ID                  DT InOut
0  120 2022-12-22 02:12:12    IN
1  120 2022-12-23 04:12:46   OUT
2  120 2022-12-26 08:11:12    IN
3  120 2022-12-30 02:12:13   OUT
英文:

Convert DT column to datetime format and truncate seconds to two(2) digits, after that use .loc to add a second if a condition is met

df['DT'] = (pd.to_datetime(df['DT'], format='%Y-%m-%d-%H:%M:%S%f').dt.floor('s')
              .apply(lambda x: x.replace(microsecond=(x.microsecond // 1000) * 1000)))
df.loc[df['InOut'] == 'OUT', 'DT'] += pd.Timedelta(seconds=1)

    ID                  DT InOut
0  120 2022-12-22 02:12:12    IN
1  120 2022-12-23 04:12:46   OUT
2  120 2022-12-26 08:11:12    IN
3  120 2022-12-30 02:12:13   OUT

答案3

得分: 0

请将您的 DT 列调整为有效的日期时间格式,使用 pd.to_datetime,然后再加上 1 秒,使用 pd.Timedelta(seconds=1)

df['DT'] = pd.to_datetime(df['DT'], format='%Y-%m-%d %H:%M:%S', exact=False)
df.loc[df['InOut'].eq('OUT'), 'DT'] += pd.Timedelta(seconds=1)

        ID                  DT InOut
    0  120 2022-12-22 02:12:12    IN
    1  120 2022-12-23 04:12:46   OUT
    2  120 2022-12-26 08:11:12    IN
    3  120 2022-12-30 02:12:13   OUT

这是调整后的数据表。

英文:

Adjust your DT column to a valid datetime format with pd.to_datetime, then - add 1 second with pd.Timedelta(seconds=1):

df['DT'] = pd.to_datetime(df['DT'], format = '%Y-%m-%d-%H:%M:%S', exact=False)
df.loc[df['InOut'].eq('OUT'), 'DT'] += pd.Timedelta(seconds=1)

    ID                  DT InOut
0  120 2022-12-22 02:12:12    IN
1  120 2022-12-23 04:12:46   OUT
2  120 2022-12-26 08:11:12    IN
3  120 2022-12-30 02:12:13   OUT

答案4

得分: 0

import pandas as pd
import datetime
import numpy as np

df = pd.DataFrame({'ID':[120, 120, 120, 120],
                   'DT':['2022-12-22-02:12:123', 
                         '2022-12-23-04:12:456', 
                         '2022-12-26-08:11:125', 
                         '2022-12-30-02:12:126'],
                   'InOut':['IN', 'OUT', 'IN', 'OUT']
                   })

df.loc[:, ('DT')] = pd.to_datetime(df['DT'].add('0'*5), 
                                   format='%Y-%m-%d-%H:%M:%S%f') # (1)

r = df[df['InOut'].eq('OUT')].copy()
r['DT'] = r['DT'].add(datetime.timedelta(seconds=1))

df.loc[:, ('DT')] = df['DT'].mask(df['InOut'].eq('OUT'), r['DT'])

print(df)
    ID                      DT InOut
0  120 2022-12-22 02:12:12.300    IN
1  120 2022-12-23 04:12:46.600   OUT
2  120 2022-12-26 08:11:12.500    IN
3  120 2022-12-30 02:12:13.600   OUT

(1) 注意,我将原始的数据框转换为更常规的日期时间表示形式。

英文:
import pandas as pd
import datetime
import numpy as np

df = pd.DataFrame({'ID':[120, 120, 120, 120],
                   'DT':['2022-12-22-02:12:123', 
                         '2022-12-23-04:12:456', 
                         '2022-12-26-08:11:125', 
                         '2022-12-30-02:12:126'],
                   'InOut':['IN', 'OUT', 'IN', 'OUT']
                   })

df.loc[:, ('DT')] = pd.to_datetime(df['DT'].add('0'*5), 
                                   format='%Y-%m-%d-%H:%M:%S%f') # (1)

r = df[df['InOut'].eq('OUT')].copy()
r['DT'] = r['DT'].add(datetime.timedelta(seconds=1))

df.loc[:, ('DT')] = df['DT'].mask(df['InOut'].eq('OUT'), r['DT'])

print(df)
    ID                      DT InOut
0  120 2022-12-22 02:12:12.300    IN
1  120 2022-12-23 04:12:46.600   OUT
2  120 2022-12-26 08:11:12.500    IN
3  120 2022-12-30 02:12:13.600   OUT

(1) Note I turned original df into a more conventional datetime presentation

huangapple
  • 本文由 发表于 2023年3月4日 04:26:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75631594.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定