Issue
I do not understand why this solution does not work to move forward one day in some rows of the series, working on the index.
curva_generacion.loc[curva_generacion.index.hour == 0].index = curva_generacion.loc[curva_generacion.index.hour == 0].index - pd.DateOffset(days=1)
This is my DataFrame, and with the previous solution I am unable to change the data:
**SVH.2.1 BIS**
**FECHA**
2015-01-01 00:00:00 0.00
2015-01-01 01:00:00 0.00
2015-01-01 02:00:00 0.00
[...]
2015-01-01 21:00:00 0.00
2015-01-01 22:00:00 0.00
2015-01-01 23:00:00 0.00
2015-01-02 00:00:00 0.00
2015-01-02 01:00:00 0.00
2015-01-02 02:00:00 0.00
When it is 0:00, the table includes the data in the following day. To be compatible with the table I already work with, the 0:00 data must belong to the previous day.
I have managed to filter well the data I need to change and subtract a day from it, but the index is not modified.
EDIT
I think I have a conceptual error that I have seen after testing the following code.
auxiliar1 = curva_generacion.loc[curva_generacion.index.hour == 0]
auxiliar1.index = auxiliar1.index - pd.DateOffset(days=1)
auxiliar1.index = auxiliar1.index.map(lambda t: t.replace(year=2015))
auxiliar2 = curva_generacion.loc[curva_generacion.index.hour != 0]
curva_generacion = pd.concat([auxiliar1, auxiliar2])
curva_generacion.sort_index(inplace=True)
With this code I extract the data from which I want to subtract a day, I do the operation, and I concatenate them and one with the data that I have not modified. When I re-sort the index, Python puts back as the first data of the day the one corresponding to 0:00.
What I really need is to sort an index where the first data of the day is the 1:00 a.m. data, and the last is the 0:00 data.
Solution
I can suggest an option with resetting the index, setting the required values and then installing the index.
import pandas as pd
curva_generacion = curva_generacion.reset_index()
ind = curva_generacion['**FECHA**'].dt.hour == 0
curva_generacion.loc[ind, '**FECHA**'] = (curva_generacion.loc[ind, '**FECHA**'] -
pd.DateOffset(days=1))
curva_generacion = curva_generacion.set_index('**FECHA**')
print(curva_generacion)
Output:
**SVH.2.1 BIS**
**FECHA**
2014-12-31 00:00:00 1
2015-01-01 01:00:00 2
2015-01-01 02:00:00 3
2015-01-01 21:00:00 4
2015-01-01 22:00:00 5
2015-01-01 23:00:00 6
2015-01-01 00:00:00 7
2015-01-02 01:00:00 8
2015-01-02 02:00:00 9
Update:
Updating indexes without resetting.
And an error when setting individual values: 'TypeError: Index does not support mutable operations'
. Rather, it concerns the installation of individual values, since if you reinstall everything, this will complete successfully.
import numpy as np
import pandas as pd
ind = curva_generacion.index.hour == 0
val_index = curva_generacion.index.values.copy()
val_index[ind] = val_index[ind] - np.timedelta64(1,'D')
curva_generacion.index = val_index
Answered By - inquirer
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.