0

I've got data in excel, one sheet has dates as columns, the other - as rows. Using pd.read_excel I get them as dataframes. The type of one (Data1) is datetime.datetime, the other (Data2) is timestamp.

  1. How do I prevent this from happening and get uniform types?

I want to do

Data1[Data1.index<Data2.columns[-1]]

this returns an error

TypeError: '<=' not supported between instances of 'datetime.time' and 'Timestamp'

I tried doing

datetime(Data2.columns[-1].year,Data2.columns[-1].month,Data2.columns[-1].day) 

instead of

Data2.columns[-1]

the error changes to

TypeError: '<=' not supported between instances of 'datetime.time' and 'datetime.datetime'

  1. I'm lost, how do I get the types to match? What's this datetime.time type out of thin air.

EDIT

this seems to be the crux of the issue:

Data1[Data1.index<Data1.index[3]]

returns

TypeError: '<' not supported between instances of 'datetime.time' and 'datetime.datetime'

Data type for Data1.index is datetime.time.

3
  • Both error messages suggest that the type of Data1 is in fact datetime.time. Commented Aug 19, 2020 at 13:32
  • Curiously, this produces the same error message Commented Aug 19, 2020 at 13:36
  • Sorry, I have zero experience with pandas. But to convert between datetimes and timestamp floats, you can do dt = datetime.fromtimestamp(ts) and ts = dt.timestamp(). Commented Aug 19, 2020 at 13:49

1 Answer 1

1

Make sure to compare apples with apples. Either compare full datetime (date and time component) or only one of each. Ex:

from datetime import datetime, timedelta
import pandas as pd

# date and time:
pd.Timestamp('now') >= datetime.now() + timedelta(hours=1)
# False

# time only:
pd.Timestamp('now').time() >= (datetime.now() + timedelta(hours=1)).time()
# False

# date only:
pd.Timestamp('now').date() >= (datetime.now() + timedelta(hours=1)).date()
# True

as opposed to e.g.

pd.Timestamp('now').time() >= (datetime.now() + timedelta(hours=1))

...will throw

TypeError: '>=' not supported between instances of 'datetime.time' and 'datetime.datetime'

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

1 Comment

Thanks, this was precisely the problem, turns out I had a few datetime.time() in there and was comparing datetime.datetime with datetime.time.

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.