<gh_stars>0

import math

import pandas as pd

EUR = 'EUR'
TX_TYPES = {'Pikaosto': 'buy',
            'Pikamyynti': 'sell',
            'Myyntitarjous': 'sell',
            'Holvi-talletus': 'stash',
            'Holvi-nosto': 'unstash',
            'Palkkio': 'receive',
            'Lähetetty maksu': 'spend',
            'Vastaanotettu maksu': 'receive'}


def read_coinmotion(csv, year=0, until=None, asset='BTC'):
    df = pd.read_csv(csv, parse_dates=['Date'], dayfirst=True)
    #df = df[df.Account.apply(lambda x: x in [asset, 'EUR'])]
    df = df[df.Status=='Valmis']
    if year:
        df = df[df.Date.apply(lambda t: t.year==year)]
    if until:
        df = df[df.Date<=until]
    return df


def drop_asset(df, asset):
    txs = df[df.Account == asset]
    for i, tx in txs.iterrows():
        selection = (df['Account']=='EUR') & (df['Date']==tx['Date']) & (df['Type']==tx['Type'])
        df = df[-selection]
    return df[df.Account != asset]


def txtype(typein):
    try:
        return TX_TYPES[typein]
    except KeyError:
        return 'irrelevant'


def rate(ratestr):
    try:
        ratestr = ratestr.replace('(', '').replace(')','')
    except AttributeError:
        return math.nan
    return float(ratestr.split(' €')[0])



import math

import pandas as pd

EUR = 'EUR'
TX_TYPES = {'Pikaosto': 'buy',
            'Pikamyynti': 'sell',
            'Myyntitarjous': 'sell',
            'Holvi-talletus': 'stash',
            'Holvi-nosto': 'unstash',
            'Palkkio': 'receive',
            'Lähetetty maksu': 'spend',
            'Vastaanotettu maksu': 'receive'}


def read_coinmotion(csv, year=0, until=None, asset='BTC'):
    df = pd.read_csv(csv, parse_dates=['Date'], dayfirst=True)
    #df = df[df.Account.apply(lambda x: x in [asset, 'EUR'])]
    df = df[df.Status=='Valmis']
    if year:
        df = df[df.Date.apply(lambda t: t.year==year)]
    if until:
        df = df[df.Date<=until]
    return df


def drop_asset(df, asset):
    txs = df[df.Account == asset]
    for i, tx in txs.iterrows():
        selection = (df['Account']=='EUR') & (df['Date']==tx['Date']) & (df['Type']==tx['Type'])
        df = df[-selection]
    return df[df.Account != asset]


def txtype(typein):
    try:
        return TX_TYPES[typein]
    except KeyError:
        return 'irrelevant'


def rate(ratestr):
    try:
        ratestr = ratestr.replace('(', '').replace(')','')
    except AttributeError:
        return math.nan
    return float(ratestr.split(' €')[0])
#df = df[df.Account.apply(lambda x: x in [asset, 'EUR'])]