0

I can compile it. However, when I input my date and time I get the error. Below is the code in question regarding this issue. error code: TypeError: '<=' not supported between instances of 'Timestamp' and 'str'.

class evaluation(): def __init__(self, data): self.data = data self.data['Date'] = pd.to_datetime(self.data['Date']) # Will receive 'actual' datetime from df, and user defined 'start' and 'stop' datetimes. def in_range(self, actual, start, stop): return start <= actual <= stop def evaluate(self): user_start = pd.to_datetime(input("Enter your start date (yyyy-mm-dd hour:min:second): ")) user_stop = pd.to_datetime(input("Enter your end date (yyyy-mm-dd hour:min:second): ")) # creates series of True or False selecting proper rows. print(self.data['Date'].dtype) mask = self.data['Date'].apply(self.in_range, args=(user_start, user_stop)) # Do the groupby and sum on only those rows. montant_init = self.data.loc[mask].groupby("Initiateur")["Montant (centimes)"].sum() print(montant_init) 

traceback error:

 Traceback (most recent call last): File "C:\Users\DELL\PycharmProjects\vps_project\main.py", line 31, in <module> init_evalobj.evaluations() File "C:\Users\DELL\PycharmProjects\vps_project\main.py", line 24, in evaluations self.evaluate() File "C:\Users\DELL\PycharmProjects\vps_project\evaluation.py", line 20, in evaluate mask = self.data['Date'].apply(self.in_range, args=(user_start, user_stop)) File "C:\Users\DELL\PycharmProjects\vps_project\venv\lib\site-packages\pandas\core\series.py", line 4138, in apply mapped = lib.map_infer(values, f, convert=convert_dtype) File "pandas\_libs\lib.pyx", line 2467, in pandas._libs.lib.map_infer File "C:\Users\DELL\PycharmProjects\vps_project\venv\lib\site-packages\pandas\core\series.py", line 4123, in f return func(x, *args, **kwds) File "C:\Users\DELL\PycharmProjects\vps_project\evaluation.py", line 13, in in_range return start <= actual <= stop TypeError: '<=' not supported between instances of 'Timestamp' and 'str' Process finished with exit code 1 
 output: Enter your start date (yyyy-mm-dd hour:min:second): 2021.06.06 11:00:00 Enter your end date (yyyy-mm-dd hour:min:second): 2021.06.06 12:00:00 object + traceback error 

thanks

8
  • What is the output of print(self.data['Date']) if you put it right above mask = self.data['Date'].apply...? Commented Jun 15, 2021 at 8:29
  • output: 0 09.06.2021 11:19:09 1 09.06.2021 11:19:02 2 09.06.2021 11:18:56 3 09.06.2021 11:18:53 4 09.06.2021 11:18:38 ... 95 08.06.2021 11:12:40 96 08.06.2021 11:12:40 97 08.06.2021 11:12:39 98 07.06.2021 11:12:25 99 07.06.2021 11:12:19 Commented Jun 15, 2021 at 8:32
  • What is the output of self.data['Date'].dtype ? Commented Jun 15, 2021 at 8:33
  • it outputs object Commented Jun 15, 2021 at 8:34
  • Please add the output in the question (so it's formatted) and keep the last line too Commented Jun 15, 2021 at 8:34

1 Answer 1

1

pd.to_datetime(self.data['Date']) is not modifying self.data['Date'], you're throwing the result away, so when you reach in_range one of the arguments is still a string.

Change it to self.data['Date'] = pd.to_datetime(self.data['Date']).

Sign up to request clarification or add additional context in comments.

8 Comments

thank you for your answer; however I still have the error even after modifying as you told me sadly
Then please add the full error traceback to the question
Done; please check my newly edited question
It worked; when I wrote that line right above the mask. I don't know how it fixed it. do you mind explaining if you know?
I think you had some stale data left in your object instance. By writing it right before the line where it raised the error, you ensured the line was actually executed (compared to having it in the class initialization, where it is executed only when a new instance is created)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.