Home > Net >  How do I know what have been changed by pd.to_datetime?
How do I know what have been changed by pd.to_datetime?

Time:04-19

Assume it is a large database load from a csv, I don't know there is a "Hello" in date column.

df = {"No.": [1, 2, 3, 4],
      "Date": ["17/4/2022", "18/4/2022", "Hello", "19/4/2022"]}

Output:

    No. Date
    1   17/4/2022
    2   18/4/2022
    3   Hello
    4   19/4/2022

After df["Date"] = pd.to_datetime(df["Date"], infer_datetime_format=True, errors="coerce")

The "Hello" become "NaT".

Output:

No. Date
1   17/4/2022
2   18/4/2022
3   NaT
4   19/4/2022

How do I know there was a "Hello"?

How do I know what value have been changed by pd.to_datetime?

CodePudding user response:

As you mentioned you want to know the changes and get only strings. You can do it this way:

import pandas as pd

df = pd.DataFrame({"No.": [1, 2, 3, 4],
      "Date": ["17/4/2022", "18/4/2022", "Hello", "19/4/2022"]})
df['Changes'] = df['Date'].apply(lambda x: x if x.isalpha() else "No Change")

Output:

   No.       Date    Changes
0    1  17/4/2022  No Change
1    2  18/4/2022  No Change
2    3      Hello      Hello
3    4  19/4/2022  No Change

CodePudding user response:

There's a way to achieve what you're trying to do - you could join two dataframes by index:

df_1.join(df_2, on = df_1.index, lsuffix = '_df_1', rsuffix = '_df_2')

So the resulting table could look like this

Joined tables

  • Related