Search code examples
pythonpandastime-seriesfinance

Multiplication of selected part of a Dataframe


I am currently cleaning a financial dataset, made up of different price timeseries of stocks. Some of the timeseries include a so called "Stock Split", this means that number of shares has changed. To give an example: The number of shares of the stock below have been halfed in 2017

enter image description here

To correct this problem, I would like to multiply the whole timeseries before this split event with 0.5. To do so, I did the following. df is the dataframe with the different stocks, and "A" is just one time series.

# searching for the stock split
df_return = df["A"].pct_change(1)                       # calculate returns
df_loc = df_return["A"].index.get_loc(df_return["A"].idxmin())   # get "location" of stock split

As a next step, I need to multiply all values from 0:df_loc in df by 0.5. I failed with approaches such as:

df = df.replace([df["A"].iloc[:df_loc]], df*2)

Does anyone know how to solve this problem? I think it should be rather easy, but I just can't figure it out. Thanks in advance


Solution

  • You might want to do this.

    df.loc[:df_loc, 'price'] *= 0.5