Home > Software engineering >  modifying dataframe through specifics dates
modifying dataframe through specifics dates

Time:11-30

I've a timeseries that i need pass a equation in the same day and month across the years

Name     | date       | value | Type
player 1 | 2010/02/10 | 100   | 2
player 2 | 2011/16/15 | 200   | 3
player 3 | 2012/02/10 | 150   | 4 
player 4 | 2013/11/16 | 136   | 5
player 5 | 2014/02/10 | 94    | 6

I need change my column 'Type' for my dates where the month and day be equal to 'YYYY/02/10' ignoring the years, and if this be a weekend use the next useful day.

My final data should be like

Name     | date       | value | Type
player 1 | 2010/02/10 | 100   | 2
player 2 | 2011/16/15 | 200   | 3
player 3 | 2012/02/10 | 150   | 2 
player 4 | 2013/11/16 | 136   | 5
player 5 | 2014/02/10 | 94    | 2

My timeseries is huge, so i cannot use a loop like for i in list where my list are all years then pass a f-string like f'{i/02/10} because i need a little more performance.

Any idea how i can do that?

CodePudding user response:

If you have strings:

df.loc[df['date'].str.endswith('02/10'), 'Type'] = 2

For datetime type:

df.loc[df['date'].dt.strftime('%d/%m').eq('02/10'), 'Type'] = 2

Output:

       Name        date  value  Type
0  player 1  2010/02/10    100     2
1  player 2  2011/16/15    200     3
2  player 3  2012/02/10    150     2
3  player 4  2013/11/16    136     5
4  player 5  2014/02/10     94     2
  • Related