简体   繁体   中英

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.

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 eg

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

...will throw

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM