Home > database >  TypeError: cannot subtract DatetimeArray from ndarray when prepending a row to dataframe
TypeError: cannot subtract DatetimeArray from ndarray when prepending a row to dataframe

Time:01-17

import pandas as pd
import numpy as np

df = pd.DataFrame({"start": [pd.to_datetime("2001-01-01 08:00:00 00:00")]})

df1 = pd.DataFrame([np.nan], columns=df.columns)
df1[["start"]] = [
    df["start"].iloc[0],
]
df = df1.append(df, ignore_index=True)

df["start"] - df["start"].dt.normalize()

produces the error

TypeError: cannot subtract DatetimeArray from ndarray

If I instead construct df1 directly

import pandas as pd
import numpy as np

df = pd.DataFrame({"start": [pd.to_datetime("2001-01-01 08:00:00 00:00")]})

df1 = pd.DataFrame([df["start"].iloc[0],], columns=df.columns)
df = df1.append(df, ignore_index=True)

df["start"] - df["start"].dt.normalize()

I don't get an error. I am wondering what is the problem with the first approach?

CodePudding user response:

When you are creating the df1 dataframe using np.nan, it creates the "start" column as a float dtype.

And when you are trying to assign the value of df["start"].iloc[0] to df1["start"], it tries to convert the timestamp object to a float and raises an error.

The second approach is working fine because it creates the "start" column with datetime dtype and assigns the timestamp object to it.

I think you could instantiate the column as a datetime by doing

df1 = pd.DataFrame([pd.NaT], columns=df.columns)

Hope this helps ! Have a nice day

CodePudding user response:

I just realised.

df1[["start"]] = [
    df["start"].iloc[0],
] 

This line, I think you meant

df1["start"] = [
    df["start"].iloc[0],
]

Peace

  • Related