I have a dataframe with only one row (in the future, it will be more, but I am just using this as an example). I am trying to grab the "SETTLEMENT_DATE" column.
print(previous["SETTLEMENT_DATE"])
0 2021-06-22 00:00:00.0
Name: SETTLEMENT_DATE, dtype: object
.
In order to get the date, I did list(previous["SETTLEMENT_DATE"])[0]
, which yields: '2021-06-22 00:00:00.0'
. Now I turn this into a date format using:
def create_datetime_object(pd_object):
date_time_str = list(pd_object)[0]
return datetime.strptime(date_time_str, "%Y-%m-%d %H:%M:%S.%f")
This code: create_datetime_object(previous["SETTLEMENT_DATE"])
yields: datetime.datetime(2021, 6, 22, 0, 0)
In the future, I will have multiple rows of data, so I wanted to use pd.apply() to apply this function to the entire column. But when I do that, I get:
previous["SETTLEMENT_DATE"] = previous["SETTLEMENT_DATE"].apply(create_datetime_object)
ValueError: time data '2' does not match format '%Y-%m-%d %H:%M:%S.%f
Does anyone know why I am getting this error and how to solve?
这应该工作:
previous["SETTLEMENT_DATE"] = previous.apply(lambda r : create_datetime_object(r.SETTLEMENT_DATE) , axis=1)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.