Home > Software engineering >  How to float to object in pandas?
How to float to object in pandas?

Time:03-21

I have the following dataframe: all_data

delay   settled_users   due_amt prime_tagging   pending_users   cycle_end_date
    0.0   114351    8.095711e 07    Prime_Super 236899           2022-03-15
    1.0   160691    5.590400e 07    Prime_Super 190559           2022-03-15
    2.0   211160    5.818422e 07    Prime_Super 140090           2022-03-15
    3.0   270745    7.271832e 07    Prime_Super 80505             2022-03-15

all_data.info()

<class 'pandas.core.frame.DataFrame'>
Int64Index: 30 entries, 1 to 6
Data columns (total 6 columns):
delay             30 non-null float64
settled_users     30 non-null int64
due_amt           30 non-null float64
prime_tagging     30 non-null object
pending_users     30 non-null int64
cycle_end_date    30 non-null object
dtypes: float64(2), int64(2), object(2)

I want a new column all_data['delay'] all_data['cycle_end_date'] but this throws TypeError: unsupported operand type(s) for : 'int' and 'datetime.date'

How do I achieve this? Please help

pd.to_timedelta(all_data['delay'])   pd.to_datetime(all_data['cycle_end_date'].astype(str))

or the other one results

1   2022-03-15 00:00:00.000000000
2   2022-03-15 00:00:00.000000001
3   2022-03-15 00:00:00.000000002
4   2022-03-15 00:00:00.000000003
5   2022-03-15 00:00:00.000000004
6   2022-03-15 00:00:00.000000005
1   2022-03-15 00:00:00.000000000

CodePudding user response:

Use to_timedelta with unit='d' for days and add values to datetimes:

all_data['new'] = pd.to_timedelta(all_data['delay'], unit='d')   pd.to_datetime(all_data['cycle_end_date'])

Or:

all_data['new'] = pd.to_timedelta(all_data['delay'], unit='d')   pd.to_datetime(all_data['cycle_end_date'].astype(str))
  • Related