Vanessa_C
Vanessa_C

Reputation: 77

How to call python function which has arguments in date format?

So, I have the function below, and the problem is that when I call the function I get the error saying ValueError: could not convert string to Timestamp (unfortunately, pc wouldn't allow me copy the full error message though, because pc at work is just built that way :( ). Anyway, here's my code:

def ao_year(start, end):
    ##This fucntion selects all columns and rows, between two selected 
    ##dates from 'event_start_date_time' column
    return ao_paths_2009.loc[(ao_paths_2009['event_start_date_time'] >= 
         '{}') & (ao_paths_2009['event_start_date_time'] <= '{}')]

#Calling the function
ao_year_2009 = ao_year('2009-01-01', '2009-12-31')
print(ao_year_2009.info())

I've already tried .strftime('%d/%m%)Y' without success.

Upvotes: 0

Views: 3964

Answers (1)

oetzi
oetzi

Reputation: 1052

# option 1: using strongly typed argument
from datetime import date
import pandas as pd


def ao_year(start: date, end: date):
    return ao_paths_2009.loc[
        (ao_paths_2009['event_start_date_time'] >= start) & (ao_paths_2009['event_start_date_time'] <= end)]


try:
    _list = {'event_start_date_time': ['2009-01-01 12:44:33 PM',
                                       '2009-01-02 12:44:33 PM',
                                       '2009-01-03 12:44:33 PM',
                                       '2009-01-04 12:44:33 PM',
                                       '2009-01-05 12:44:33 PM',
                                       '2009-01-06 12:44:33 PM',
                                       '2009-01-07 12:44:33 PM',
                                       '2009-01-08 12:44:33 PM',
                                       '2009-01-09 12:44:33 PM',
                                       '2009-01-10 12:44:33 PM',
                                       '2009-01-11 12:44:33 PM',
                                       '2009-01-12 12:44:33 PM',
                                       '2009-01-13 12:44:33 PM',
                                       '2009-01-14 12:44:33 PM',
                                       '2009-02-15 12:44:33 PM',
                                       '2009-02-16 12:44:33 PM']}
    ao_paths_2009 = pd.DataFrame(data=_list)
    ao_year_2009 = ao_year('2009-01-01', '2009-12-31')
    # print(ao_year_2009.info())
    with pd.option_context('display.max_rows', None, 'display.max_columns', None):  # more options can be specified also
        print(ao_year_2009)
except Exception as e:
    print(e)

# option 2: pass function param as string convert to datetime
import pandas as pd


def ao_year(start, end):
    start = pd.to_datetime(start).date()
    end = pd.to_datetime(end).date()
    return ao_paths_2009.loc[
        (ao_paths_2009['event_start_date_time'] >= start) & (ao_paths_2009['event_start_date_time'] <= end)]


try:
    _list = {'event_start_date_time': ['2009-01-01 12:44:33 PM',
                                       '2009-01-02 12:44:33 PM',
                                       '2009-01-03 12:44:33 PM',
                                       '2009-01-04 12:44:33 PM',
                                       '2009-01-05 12:44:33 PM',
                                       '2009-01-06 12:44:33 PM',
                                       '2009-01-07 12:44:33 PM',
                                       '2009-01-08 12:44:33 PM',
                                       '2009-01-09 12:44:33 PM',
                                       '2009-01-10 12:44:33 PM',
                                       '2009-01-11 12:44:33 PM',
                                       '2009-01-12 12:44:33 PM',
                                       '2009-01-13 12:44:33 PM',
                                       '2009-01-14 12:44:33 PM',
                                       '2009-02-15 12:44:33 PM',
                                       '2009-02-16 12:44:33 PM']}
    ao_paths_2009 = pd.DataFrame(data=_list)
    ao_paths_2009['event_start_date_time'] = pd.to_datetime(ao_paths_2009['event_start_date_time'])
    ao_year_2009 = ao_year('2009-01-01', '2009-12-31')
    with pd.option_context('display.max_rows', None, 'display.max_columns', None):  # more options can be specified also
        print(ao_year_2009)

except Exception as e:
    print(e)

Upvotes: 2

Related Questions