Syntax Error (Phyton)

Asked

Viewed 54 times

0

inserir a descrição da imagem aquiI am doing a work here with python using Pandas/Matplotlib/Seaborn/Numpy and wanted to understand why it gave syntax error. I’m trying to modify 3 columns of my dataset if String for Date Time.

datetime_cols = ['Start Time', 'End Time', 'Modified Date']
# método para transformar strings em date_time
def to_datetime(DataFrame, columns):
    for col in columns:
        DataFrame[col] = DataFrame[col].apply(lambda _: datetime.strptime(_,"%m/%d/%Y, %H:%M:%S")

to_datetime(df, datetime_cols)

- Error gives method call

  • 2

    Place the error message complete. With the error message of just tapping the eye we already inform you the problem.

  • 1

    the error that gives is only the same, what is in the post.

  • 1

    Is missing close one ) at the end of DataFrame[col] = DataFrame[col].apply(lambda _: datetime.strptime(_,"%m/%d/%Y, %H:%M:%S") and you’re using a hole _ which is a value discard object. Do DataFrame[col] = DataFrame[col].apply(lambda d: datetime.strptime(d,"%m/%d/%Y, %H:%M:%S"))

  • It was! rsrs That’s what we needed.

  • I said just send the error message!. Lie, I had to read the source to find the error, the message did not help much. :)

  • KKKKKKKKKKKKK

Show 1 more comment

1 answer

1

Even if the suggestion in the comments worked, it is not performative in large dataframes because of the use of apply(), that although it’s faster than a for index, row in df.iterrows(); is slower than vectorization. See more details here

Note: In the link I passed, do not look at the solution accepted as an answer, but for the one that received 100 points for the quality of it.

That said, it follows another solution:

Creating Dataframe for Testing

>>> import pandas as pd
>>> df = pd.DataFrame({"A": ['11/4/2021', '12/4/2021', '13/4/2021'], "B": [1, 2, 3], "C": ['1/4/2021', '2/4/2021', '3/4/2021']})

>>> df
           A  B         C
0  11/4/2021  1  1/4/2021
1  12/4/2021  2  2/4/2021
2  13/4/2021  3  3/4/2021

Checking type of columns

>>> df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 3 columns):
 #   Column  Non-Null Count  Dtype
---  ------  --------------  -----
 0   A       3 non-null      object
 1   B       3 non-null      int64
 2   C       3 non-null      object
dtypes: int64(1), object(2)
memory usage: 200.0+ bytes

Converting column A

>>> df["A"] = pd.to_datetime(df["A"], format='%d/%m/%Y')
>>> df
           A  B         C
0 2021-04-11  1  1/4/2021
1 2021-04-12  2  2/4/2021
2 2021-04-13  3  3/4/2021

Checking columns again

>>> df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 3 columns):
 #   Column  Non-Null Count  Dtype
---  ------  --------------  -----
 0   A       3 non-null      datetime64[ns]
 1   B       3 non-null      int64
 2   C       3 non-null      object
dtypes: datetime64[ns](1), int64(1), object(1)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.