Skip to content

Instantly share code, notes, and snippets.

@sjtalkar
Created October 25, 2020 01:40
Show Gist options
  • Save sjtalkar/75b33540fdff9e114fb58577e1bf2b15 to your computer and use it in GitHub Desktop.
Save sjtalkar/75b33540fdff9e114fb58577e1bf2b15 to your computer and use it in GitHub Desktop.
import pandas as pd
import datetime
# The historical data setup
assets = ['Automobiles', 'Building'] * 2
countries = ['Andorra', 'Andorra', 'Brunei', 'Brunei']
rates = [12.5, 4.5, 14.0, 6.5]
effective_from = ['2019-01-01'] * 4
effective_until = [None] * 4
depreciation_rate_df = pd.DataFrame(list(zip(assets, countries, rates, effective_from, effective_until)),
columns= ['asset', 'country','rate', 'effective_from', 'effective_until'])
# New rate change data
assets = ['Automobiles', 'Building'] * 2
countries = ['Andorra', 'Andorra', 'Brunei', 'Brunei']
rates = [15, 5, 13, 7]
effective_from = ['2020-01-01'] * 2 + ['2020-01-03'] * 2
effective_until = [None] * 4
# Add the new data to historical data
new_depreciation_rate_df = pd.DataFrame(list(zip(assets, countries, rates, effective_from, effective_until)),
columns= ['asset', 'country','rate', 'effective_from', 'effective_until'])
depreciation_rate_df = depreciation_rate_df.append(new_depreciation_rate_df, ignore_index=True)
#Change the type of the date column from Object to datetime64
depreciation_rate_df['effective_from']= depreciation_rate_df['effective_from'].astype('datetime64')
depreciation_rate_df['effective_until'] = depreciation_rate_df.groupby(['country', 'asset'])['effective_from'].shift(-1)
depreciation_rate_df['effective_until'] = depreciation_rate_df['effective_until'].apply(lambda x: x-datetime.timedelta(seconds=1) if ~pd.isnull(x) else None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment