0

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.

2
  • Update: I don't know why comments is cutting off first line f my code, here it is: 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()) Commented Aug 23, 2019 at 11:58
  • 1
    I don't know much about the database or what ao_paths_2009 is but if you just need to convert the string to a timestamp print(datetime.strptime('2009-01-01', '%Y-%M-%d')) works for dates in the form '2009-01-01', '2009-12-31' Commented Aug 23, 2019 at 13:28

1 Answer 1

2
# 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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.