I am performing a data transformation and I need to run a while
between date periods, but I have the error:
"TypeError: Cannot compare type 'Timestamp' with type 'str'"
I have seen type()
what type of data they are and both are class 'pandas.tslib.Timestamp'
therefore I do not understand what happens.
I am attaching my full code for you to see and a CSV file for you to run and test:
from collections import defaultdict
import pandas as pd
from datetime import datetime, timedelta
from time import time
fecha_i=[]
fecha_f=[]
contador=[]
id=[]
contador={}
contador = defaultdict(list)
df = pd.read_csv('contour-export-2017-12-14.csv', header=0, sep=',',parse_dates = ['FCH_HORA_INICIO'],dayfirst = True, usecols=[0,3,6,7])
fecha_i=df['FCH_HORA_INICIO']
fecha_f=df['FCH_HORA_TERMINO']
id=df['ID']
acumulado=fecha_i[0]
i=1
k=0
print(type(acumulado))
print(type(fecha_i[0]))
while(acumulado<datetime.now()):
acumulado=fecha_i[0]+timedelta(days=i)
k=0
while k<=len(df)-1:
if acumulado>=fecha_i[k] and acumulado<=fecha_f[k]:
contador[acumulado].append(str(ID[k]))
k=k+1
else:
k=k+1
i=i+1
File: https://drive.google.com/file/d/18sKmsg9MSs_t1JWKRYArxsVP4OPud4jL/view?usp=sharing
When converting the data to time through parse_dates, it is converted to
Timestamp
, this type of data is not used to make comparisons, what you should do is convert it to datetime through the to_datetime() function:Code:
The problem is that you are not parsing the column
'FCH_HORA_TERMINO'
because you do not indicate it in the argumentparse_dates
, so the data type that the column containsFCH_HORA_TERMINO
is Python strings, while itFCH_HORA_INICIO
is a column of typedatetime64
(NumPy).Just tell a to
parse_dates
also take this column into account as a date and parse it todatetime64[ns]
:Both columns are parsed taking into account that the day comes before the month in both columns.