the_dude
the_dude

Reputation: 61

Problem with date types after excel upload (converting timestamp to datetime)

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.

Upvotes: 0

Views: 124

Answers (1)

FObersteiner
FObersteiner

Reputation: 25564

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'

Upvotes: 1

Related Questions