Home > Software design >  trying to extract month but got an error integer argument expected, got float
trying to extract month but got an error integer argument expected, got float

Time:11-06

Data Image

data.info() data['Timestamp']=pd.to_datetime(data['Timestamp'], errors='coerce')

import datetime as dt 
#function for month
def get_month(x):
    return dt.datetime(x.year, x.month,1)
#apply the function 
data['get_Timestamp'] = data['Timestamp'].apply(get_month)
data.tail()

i want get my timestamp month like 2022-10-01

CodePudding user response:

To extract month usually you can extract via the below snippet

df['get_Timestamp'] = df['ArrivalDate'].dt.month

CodePudding user response:

You use errors='coerce'. This means that there can be Nat values ​​in the dataset. You may be getting an error because the function you are using is trying to convert the Nat value. You can set the function to apply to non-nat values (like if x==pd.isnull). Also you can use this instead of function.

data['get_Timestamp'] = data['Timestamp'].dt.strftime('%Y-%m-01')
  • Related