I am working with a data frame which has a date column and I have to find the last working date of every month, the code I used works but it doesn't make sense how it worked
The data frame 'apple' had 6 columns initially but I am mainly concerned with the 'Date' column which has dates ranging from the year 2014-1980 of every month Sample Data:
Date Open High Low Close Volume Adj Close
0 2014-07-08 96.27 96.80 93.92 95.35 65130000 95.35
1 2014-07-07 94.14 95.99 94.10 95.97 56305400 95.97
2 2014-07-03 93.67 94.10 93.20 94.03 22891800 94.03
3 2014-07-02 93.87 94.06 93.09 93.48 28420900 93.48
4 2014-07-01 93.52 94.07 93.13 93.52 38170200 93.52
from pandas.tseries.offsets import MonthEnd
apple['Last_Day']=pd.to_datetime(apple['Date'],format="%Y-%m")+MonthEnd(0)
banana=apple.loc[-apple.Last_Day.duplicated()]
I expected the newly created 'Last_Day' column will have the last day of every month which I got but surprisingly the 'Date' column got the last working day of every month which I didn't understand cause I did not initialize anything to 'Date' so how did all the values in 'Date' get replaced by last working day, Output:
Date Open High Low Close Volume Adj Close Last_Day
0 2014-07-08 96.27 96.80 93.92 95.35 65130000 95.35 2014-07-31
5 2014-06-30 92.10 93.73 92.09 92.93 49482300 92.93 2014-06-30
26 2014-05-30 637.98 644.17 628.90 633.00 141005200 90.43 2014-05-31
47 2014-04-30 592.64 599.43 589.80 590.09 114160200 83.83 2014-04-30
68 2014-03-31 539.23 540.81 535.93 536.74 42167300 76.25 2014-03-31
89 2014-02-28 529.08 532.75 522.12 526.24 92992200 74.76 2014-02-28
108 2014-01-31 495.18 501.53 493.55 500.60 116199300 70.69 2014-01-31