Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass Pandas DataFrame as input #16

Open
truebrew opened this issue Sep 29, 2020 · 4 comments
Open

Pass Pandas DataFrame as input #16

truebrew opened this issue Sep 29, 2020 · 4 comments

Comments

@truebrew
Copy link

truebrew commented Sep 29, 2020

Does it sound like a good idea to accept a Pandas DataFrame with Datetimes as indices and values as column as input? It would save the step of converting to dictionary.

@tarioch
Copy link
Owner

tarioch commented Oct 1, 2020

sounds like a good idea (as far as I can tell we have an indirect dependency to pandas through scipy anyway), feel free to contribute that.

@truebrew
Copy link
Author

truebrew commented Oct 3, 2020

I have no idea how this works :). I'd suggest to pass an argument 'df' to the function, which by default will be False. Otherwise, when df=True, we can just do df.to_dict() requiring that the datetimes are the index and the values are set in the column. It will more of a series than a dataframe, actually.

@hernanhevia
Copy link

Hi, you'd give this a try:

def xirr(df, date_column='date', amount_column='amount'):
valuesPerDate = df[[date_column,amount_column]].set_index(date_column).to_dict(orient='dict')[amount_column]

Cheers

@Kk-ships
Copy link

@truebrew May be you can try this approach.

import dateparser
import xirr
def try_parsing_date(text):
    """
    try and parse date
    :param text: string
    :return: date part of datetime object
    """
    try:
        return dateparser.parse(text).date()
    except:
        pass


def xirr_df(df, date = 'date', amount= 'amount'):
      dates = df[date].tolist()
      amounts = df[amount].tolist()
      dates_parsed = list(map(try_parsing_date, dates))
      valuesPerDate = dict(zip(dates_parsed, amounts))
      return xirr.xirr(valuesPerDate)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants