python - Subtract a year if the previous month was January -




after processing able create below dataframe. problem year incorrect. date in decreasing order each location. after 2015-01-15 should 2014-12-15, not 2015-12-15.

+--------------------+---------------+-------+ |   location         | date          | value | +--------------------+---------------+-------+ | india              | 2015-03-15    |   -200| | india              | 2015-02-15    |  140  | | india              | 2015-01-15    |  155  | | india              | 2015-12-15    |   85  | | india              | 2015-11-15    |   45  | | china              | 2015-03-15    |   199 | | china              | 2015-02-15    |  164  | | china              | 2015-01-15    |  209  | | china              | 2015-12-15    |   24  | | china              | 2015-11-15    |   11  | | russia             | 2015-03-15    |   48  | | russia             | 2015-02-15    |  104  | | russia             | 2015-01-15    |  106  | | russia             | 2015-12-15    |   -20 | | russia             | 2015-11-15    |   10  | 

making strong assumption these monthly dates ending on 15th of every month , first value given location correct, can step backwards monthly location.

# create original dataframe. df = pd.dataframe({'location': ['india'] * 5 + ['china'] * 5 + ['russia'] * 5,                    'date': ['2015-03-15', '2015-02-15', '2015-01-15', '2015-12-15', '2015-11-15'] * 3,                    'value': [-200, 140, 155, 85, 45, 199, 164, 209, 24, 11, 48, 104, 106, -20, 10]})[     ['location', 'date', 'value'] ] # convert dates pandas timestamps. df['date'] = pd.datetimeindex(df['date'])  gb = df.groupby(['location'])['date'] df['date'] = [     str(first_period - months) + '-15'      location_months, first_period in zip(          gb.count(), gb.first().apply(lambda date: pd.period(date, 'm')))       months in range(location_months) ] >>> df    location        date  value 0     india  2015-03-15   -200 1     india  2015-02-15    140 2     india  2015-01-15    155 3     india  2014-12-15     85 4     india  2014-11-15     45 5     china  2015-03-15    199 6     china  2015-02-15    164 7     china  2015-01-15    209 8     china  2014-12-15     24 9     china  2014-11-15     11 10   russia  2015-03-15     48 11   russia  2015-02-15    104 12   russia  2015-01-15    106 13   russia  2014-12-15    -20 14   russia  2014-11-15     10 

the final dates in string form may again wish convert timestamps via:

df['date'] = pd.datetimeindex(df['date']) 




wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -