Skip to content

Commit

Permalink
[ibtax.py] fix prepare_fees_report
Browse files Browse the repository at this point in the history
in case we have this fee dataframe:
```
   N       date  amount                                        description  tax_year      rate  amount_rub abs_amount_del
0  1 2020-10-05  -0.01$  Other Fees - M******44:US CONSOLIDATED SNAPSHO...      2020  78.0915₽  -0.780915₽           0.01
1  2 2020-10-05   0.01$  Other Fees - M******44:US CONSOLIDATED SNAPSHO...      2020  78.0915₽   0.780915₽           0.01
```
after removing duplicates it becomes empty, so we'll face an error like this:
```
Traceback (most recent call last):
  File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/home/eugeneleko/git/investments/investments/ibtax/__main__.py", line 3, in <module>
    main()
  File "/home/eugeneleko/git/investments/investments/ibtax/ibtax.py", line 307, in main
    fees_report = prepare_fees_report(fees, cbr_client_usd, args.verbose) if fees else None
  File "/home/eugeneleko/git/investments/investments/ibtax/ibtax.py", line 102, in prepare_fees_report
    df['amount_rub'] = df.apply(lambda x: cbr_client_usd.convert_to_rub(x['amount'], x[operation_date_column]), axis=1)
  File "/home/eugeneleko/.local/lib/python3.10/site-packages/pandas/core/frame.py", line 3645, in __setitem__
    self._set_item_frame_value(key, value)
  File "/home/eugeneleko/.local/lib/python3.10/site-packages/pandas/core/frame.py", line 3788, in _set_item_frame_value
    self._set_item_mgr(key, arraylike)
  File "/home/eugeneleko/.local/lib/python3.10/site-packages/pandas/core/frame.py", line 3802, in _set_item_mgr
    self._mgr.insert(len(self._info_axis), key, value)
  File "/home/eugeneleko/.local/lib/python3.10/site-packages/pandas/core/internals/managers.py", line 1235, in insert
    raise ValueError(
ValueError: Expected a 1D array, got an array with shape (0, 6)
```
So I suggest to move df['amount_rub'] calculation before removing duplicates
  • Loading branch information
FlameFactory committed Apr 25, 2022
1 parent c7feb93 commit 916cd9b
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion investments/ibtax/ibtax.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ def prepare_fees_report(fees: List[Fee], cbr_client_usd: cbr.ExchangeRatesRUB, v
]
df = pandas.DataFrame(df_data, columns=['N', operation_date_column, 'amount', 'description', 'tax_year'])
df['rate'] = df.apply(lambda x: cbr_client_usd.get_rate(x['amount'].currency, x[operation_date_column]), axis=1)
df['amount_rub'] = df.apply(lambda x: cbr_client_usd.convert_to_rub(x['amount'], x[operation_date_column]), axis=1)

if not verbose:
df['abs_amount_del'] = df.apply(lambda x: abs(x.amount.amount), axis=1)
df.drop_duplicates(subset=[operation_date_column, 'description', 'abs_amount_del'], keep=False, inplace=True)
df.drop(columns=['abs_amount_del'], inplace=True)
df['N'] = range(1, len(df) + 1)

df['amount_rub'] = df.apply(lambda x: cbr_client_usd.convert_to_rub(x['amount'], x[operation_date_column]), axis=1)
return df


Expand Down

0 comments on commit 916cd9b

Please sign in to comment.