Home > Software design >  Adding different days in a DataFrame with a fixed date
Adding different days in a DataFrame with a fixed date

Time:06-23

I have a DataFrame with numbers ('number') and I wanted to add these numbers to a date. Unfortunately my attempts don't work and I always get error messages no matter how I try....

This is a code example how I tried it:

from datetime import datetime
number = pd.DataFrame({'date1': ['7053','0','16419','7112','-2406','2513','8439','-180','13000','150','1096','15150','3875','-10281']})
number

df = datetime(2010, 1, 1)   number['date1']
df

As an example of the result (YYYY/MM/DD) should come out a column or DataFrame with a date, which results from the calculation "start date number".

result = pd.DataFrame({'result': ['2001/03/01','1981/11/08','1975/04/08','2023/05/02']})
result

Currently the numbers are in the df 'number' type object. Then I get this error message.

unsupported operand type(s) for  : 'numpy.ndarray' and 'Timestamp'

If I change df 'number' to str or int64, I get this error message.

addition/subtraction of integers and integer-arrays with timestamp is no longer supported. instead of adding/subtracting `n`, use `n * obj.freq`

What am I doing wrong or can someone help me?

Thanks a lot!

CodePudding user response:

number['date1'] = pd.to_datetime(number['date1'].astype(int), unit='d', origin='2010/01/01')
result = number['date1'].dt.strftime('%Y/%m/%d')
print (result)

0     2029/04/24
1     2010/01/01
2     2054/12/15
3     2029/06/22
4     2003/06/01
5     2016/11/18
6     2033/02/08
7     2009/07/05
8     2045/08/05
9     2010/05/31
10    2013/01/01
11    2051/06/25
12    2020/08/11
13    1981/11/08
Name: date1, dtype: object

CodePudding user response:

If need add days by original column to 2010-01-01 use to_datetime:

number['date1'] = pd.to_datetime(number['date1'].astype(int), unit='d', origin='2010-01-01')
print (number)
        date1
0  2029-04-24
1  2010-01-01
2  2054-12-15
3  2029-06-22
4  2003-06-01
5  2016-11-18
6  2033-02-08
7  2009-07-05
8  2045-08-05
9  2010-05-31
10 2013-01-01
11 2051-06-25
12 2020-08-11
13 1981-11-08

For format YYYY/MM/DD add Series.dt.strftime:

number['date1'] = pd.to_datetime(number['date1'].astype(int), unit='d', origin='2010-01-01').dt.strftime('%Y/%m/%d')
print (number)

         date1
0   2029/04/24
1   2010/01/01
2   2054/12/15
3   2029/06/22
4   2003/06/01
5   2016/11/18
6   2033/02/08
7   2009/07/05
8   2045/08/05
9   2010/05/31
10  2013/01/01
11  2051/06/25
12  2020/08/11
13  1981/11/08
  • Related