Home > front end >  Replace start date for a given set of csv date data using panda
Replace start date for a given set of csv date data using panda

Time:09-17

python beginner here, I have a csv file that has one year data, starting at 1/1/2000, I am trying to convert the start date of the data to a specified date, 26/08/2020. The code I currently have replaces the date but it starts 1/1/2020 instead of 26/08/2020

startdate = pd.to_datetime(startdate, dayfirst = True)
data[date] = pd.to_datetime(data[date])
data[date] = [f.replace(year = startdate.year) for f in data[date]]
data = data.set_index(date)
data = data.astype('float')

and I was wondering if there is a way to match the start date with the given data set, so if my start date is in 1/8/2018, I start replacing data from the csv file from 1/8/2000, so the final date range will be from 1/8/2018 to 31/8/2019.

Input:

01/01/2000 00:000

01/01/2000 01:000

.........

31/12/2000 23:000

Expected output

26/08/2018 00:000

26/08/2018 01:000

. . . . .

26/08/2019 23:000

Thank you for your help :)

CodePudding user response:

pd.to_datetime(timestamps,day_first=True)   datetime.timedelta(days=664249) 

I believe would give you want you want ... just offset it by the difference in dates

CodePudding user response:

Somethin' like that:

import pandas as pd

date_old = pd.to_datetime("01/01/2000 00:000")
date_new = pd.to_datetime("26/08/2018 00:000")

delta = date_new - date_old

days = 366

result = [date_old   delta   pd.Timedelta(days=days) for day in range(days)]

  • Related