I want to know how to execute the conditions over time in Python.

Asked 2 years ago, Updated 2 years ago, 96 views

I learned Python for the first time and started working on a project. There are still many things I don't know, so it's a meaningful activity to try, but I'm asking you because it's not progressing any more even after searching.

From the given data, we tried to create the code below to remove the seconds in the time column and then transform it into 15 for less than 30 minutes and 45 for more than 30 minutes, but it didn't run.

print(df['time'][1].After seeing the true value in the minute<10) part

After the for statement ifdf['time'][i].I tried to proceed through minute >=30: but

Traceback (most recent call last):
  File "D:\Python\First Semester Project\Preprocessing.py", line 12, in <module>
    if df['time'][i].minute >=30:
AttributeError: 'str' object has no attribute 'minute'

It says. I would appreciate it if you could tell me which part went wrong and how to solve it.

import pandas as pd

import datetime as dt
#Bringing data
df = pd.read_csv ("D:\Python\\First Semester Project\\Practice.csv")

# Change minutes
df['time'] = pd.to_datetime(df['time'])
print(df['time'][1].minute<10) #Out: True

for i in range(42):
    if df['time'][i].minute >=30:
        df['time'] = df['time'].dt.strftime('%H:45')
    else:
        df['time'] = df['time'].dt.strftime('%H:15')

print(df)

For practice.csv

store_id,date,time,card_id,amount,installments,days_of_week,holyday
0,2016-12-14,18:05:31,d297bba73f,5,,2,0
0,2016-12-14,18:05:54,d297bba73f,-5,,2,0
0,2016-12-19,12:42:31,0880849c05,144,,0,0
0,2016-12-19,12:48:08,8b4f9e0e95,66,,0,0
0,2016-12-19,13:31:08,7ad237eed0,24,,0,0
0,2016-12-19,13:36:39,30b1422f77,44,,0,0
0,2016-12-19,14:00:23,6dd8dad5ab,146,,0,0
0,2016-12-19,14:01:36,a4a425c5d2,6,,0,0
0,2016-12-19,14:09:46,f213c011fe,306,,0,0
0,2016-12-19,14:13:02,a185b7b11f,51,,0,0
0,2016-12-19,14:51:28,5af79433af,26,,0,0
0,2016-12-19,15:25:32,0beb503a74,20,,0,0
0,2016-12-19,16:15:56,3f90c1e570,72,,0,0
0,2016-12-19,16:19:16,1a04d9a6eb,25,,0,0
0,2016-12-19,16:54:46,2e14c077ba,111,,0,0
0,2016-12-19,16:55:18,cd96a7b3f6,33,,0,0
0,2016-12-19,17:08:05,202b265f07,14,,0,0
0,2016-12-19,17:30:31,a6ac2a951f,50,,0,0
0,2016-12-19,17:36:09,5d7132c0ba,75,,0,0
0,2016-12-19,18:06:15,1606e4dd61,18,,0,0
0,2016-12-19,18:07:19,2c82181047,124,,0,0
0,2016-12-19,18:31:14,342f914c88,81,,0,0
0,2016-12-19,18:37:29,9efac78d9b,14,,0,0
0,2016-12-19,18:38:15,29a3af593e,18,,0,0
0,2016-12-19,18:49:08,7cdef6ec56,121,,0,0
0,2016-12-19,18:49:26,931b32930e,11,,0,0
0,2016-12-19,18:50:51,9713246b70,45,,0,0
0,2016-12-19,18:54:35,c60ecb9b3a,79,,0,0
0,2016-12-19,18:55:58,ba32ffa324,74,,0,0
0,2016-12-19,19:05:09,9bfde99e08,50,,0,0
0,2016-12-19,19:08:36,1b6c8b98b8,49,,0,0
0,2016-12-19,19:15:17,1a04d9a6eb,42,,0,0
0,2016-12-19,19:16:47,f4821f6036,64,,0,0
0,2016-12-19,19:35:42,64ac69ab19,65,,0,0
0,2016-12-19,19:36:03,246fe0d9f5,55,,0,0
0,2016-12-19,19:36:58,bf7f4c1f67,86,,0,0
0,2016-12-19,19:39:42,d7d9342abd,106,,0,0
0,2016-12-19,20:00:22,4b2592e323,121,,0,0
0,2016-12-19,21:12:24,50ae174fca,90,,0,0
0,2016-12-19,21:32:08,7eb2ed3e09,18,,0,0

python datetime pandas

2022-09-20 16:28

2 Answers

from io import StringIO
import pandas as pd

txt = """store_id,date,time,card_id,amount,installments,days_of_week,holyday
0,2016-12-14,18:05:31,d297bba73f,5,,2,0
0,2016-12-14,18:05:54,d297bba73f,-5,,2,0
0,2016-12-19,12:42:31,0880849c05,144,,0,0
0,2016-12-19,12:48:08,8b4f9e0e95,66,,0,0
0,2016-12-19,13:31:08,7ad237eed0,24,,0,0"""


def main():

    df = pd.read_csv(StringIO(txt))
    print(df)


    def conv_time(s: str):
        s_ = s[-5:]
        if s_ < "30:00":
            return s[:3] + "15:00"
        return s[:3] + "45:00"


    df["time_"] = df["time"].apply(conv_time)

    print(df)

    df["datetime"] = df["date"] + " " + df["time_"]
    df["datetime"] = pd.to_datetime(df["datetime"])


    print(df.info())
    print(df.to_markdown())

if __name__ == "__main__":
    main()    

I made an example. The final result is as follows.

In the example, the key is the conv_time function. The time column is a string, and you can compare it with "30:00" with only the last 5 characters of the string. The comparison of strings is prearranged, and numbers can be compared like regular numerical types if they have the same number of digits and format.

So we created one more column, added to the date column, converted it to datetime, and finally created datetime-type columns.


2022-09-20 16:28

If you look at the first line of the csv file:

store_id,date,time,card_id,amount,installments,days_of_week,holyday

Str error seems to be caused by conversion error because of this first line.

If the form of the base material is not consistent, work should only be done when certain conditions are met.

In this case, it seems that it can be solved by specifying that the operation is only possible through the if statement, or setting what to do if an error occurs through the try statement.

import pandas as pd

import datetime as dt
#Bringing data
df = pd.read_csv ("D:\Python\\First Semester Project\\Practice.csv")

# Change minutes
df['time'] = pd.to_datetime(df['time'])
print(df['time'][1].minute<10) #Out: True
for i in range(42):
    if df['time'][i].minute > 0 or df['time'][i].minute <= 0:
        if df['time'][i].minute >= 30:
            df['time'] = df['time'].dt.strftime('%H:45')
        else:
            df['time'] = df['time'].dt.strftime('%H:15')

print(df)

Or

for i in range(42):
    try:
        if df['time'][i].minute >= 30:
            df['time'] = df['time'].dt.strftime('%H:45')
        else:
            df['time'] = df['time'].dt.strftime('%H:15')
    except:
        pass

print(df)


2022-09-20 16:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.