0

I have the following dataframe

df = pd.DataFrame(data=[np.nan,1.34,np.nan,1,3,np.nan,np.nan,4,5,2,3,4],
           index=[datetime.date(2015, 3, 31),
                  datetime.date(2015, 3, 31),
                  datetime.date(2015, 3, 31),
                  datetime.date(2015, 3, 31),
                  datetime.date(2015, 4, 1),
                  datetime.date(2015, 4, 1),
                  datetime.date(2015, 4, 1),
                  datetime.date(2015, 4, 3),
                  datetime.date(2015, 4, 3),
                  datetime.date(2015, 4, 5),
                  datetime.date(2015, 4, 5),
                  datetime.date(2015, 4, 5)])

by running

np.unique(df.index) 

I get

array([datetime.date(2015, 3, 31), datetime.date(2015, 4, 1),
   datetime.date(2015, 4, 3), datetime.date(2015, 4, 5)], dtype=object)

i.e. the dates datetime.date(2015, 4, 2) datetime.date(2015, 4, 4) are missing

how can I shift the dates in order to have continuous dates?

I d like to have:

df

df
Out[28]: 
               0
2015-03-31   NaN
2015-03-31  1.34
2015-03-31   NaN
2015-03-31  1.00
2015-04-01  3.00
2015-04-01   NaN
2015-04-01   NaN
2015-04-02  4.00
2015-04-02  5.00
2015-04-03  2.00
2015-04-03  3.00
2015-04-03  4.00

where 2015-04-03 becomes 2015-04-02 and so on...

gabboshow
  • 5,359
  • 12
  • 48
  • 98
  • 1
    could you also post your codes for creating this sample df, it's just easier to get to the actual problem here. thanks a lot. – stucash Dec 08 '17 at 11:52
  • 1
    possible duplicate: https://stackoverflow.com/questions/19324453/add-missing-dates-to-pandas-dataframe – stucash Dec 08 '17 at 12:53
  • I edited my question providing the data – gabboshow Dec 08 '17 at 14:29
  • @stucash I don't want to add the missing dates but replace them from the first missing data – gabboshow Dec 08 '17 at 14:34
  • How do you know how many of each missing date to insert? In your example you've inserted two rows of 4/2 , but you have four rows of 3/31 and three rows of 4/1? – n3utrino Dec 08 '17 at 16:38
  • the 2 rows of 4-2 replace the 2 rows of 4-3, and the 3 rows of 4-3 added replace the 3 rows of 4-5 – gabboshow Dec 08 '17 at 20:52
  • believe it or not, I have thought hard about your question, at the moment my conclusion is : 1. it is not natively supported by pandas. here I mean we could hardly apply any trick/magic to just chain it up and it'd just work. 2. I think you should not go down this route. you should realign your data at a higher level rather than manipulating data here with pandas, or I would just change date source if that's affordable. 3. you could definitely try to write up a solution, but I don't know if that is worth the time. trust me, I've tried it for you. – stucash Dec 08 '17 at 20:58
  • @gabboshow there are ways to figure out the number of recurrence of missing dates, I wouldn't worry about that. – stucash Dec 08 '17 at 20:59
  • @stucash my question is related to this other question https://stackoverflow.com/questions/47536243/grouping-odd-and-even-days – gabboshow Dec 08 '17 at 21:04
  • the problem is that if the days are not consecutive (i.e. two odd days in a row) I need to make the second odd day an even day... – gabboshow Dec 08 '17 at 21:04

0 Answers0