. Advertisement .
..3..
. Advertisement .
..4..
I’m trying to work around this issue ”invalid comparison between dtype=datetime64[ns] and date” that I am facing here.
#import libraries
from __future__ import division
from datetime import datetime, timedelta,date
import pandas as pd
%matplotlib inline
from sklearn.metrics import classification_report,confusion_matrix
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
from sklearn.cluster import KMeans
import plotly.offline as pyoff
import plotly.graph_objs as go
from sklearn.model_selection import KFold, cross_val_score, train_test_split
#initate plotly
pyoff.init_notebook_mode()
#read data from csv and redo the data work we done before
tx_data = pd.read_csv(r'C:\Users\aayus\OneDrive\Desktop\Aayu\College Project\OnlineRetail.csv', encoding='latin1')
tx_data['InvoiceDate'] = pd.to_datetime(tx_data['InvoiceDate'])
tx_data
tx_uk = tx_data.query("Country=='United Kingdom'").reset_index(drop=True)
tx_uk
So far, everything is running smoothly. However, as soon as this code part is added. It produces a mistake.
#create 3m and 6m dataframes
tx_3m = tx_uk[(tx_uk.InvoiceDate < date(2011,6,1)) & (tx_uk.InvoiceDate >= date(2011,3,1))].reset_index(drop=True)
tx_6m = tx_uk[(tx_uk.InvoiceDate >= date(2011,6,1)) & (tx_uk.InvoiceDate < date(2011,12,1))].reset_index(drop=True)
I still haven’t used numpy or pandas, so any assistance would be greatly appreciated.
Thanks you guys!
The cause: According to the error notice,
tx uk.InvoiceDate
appears to be a datetime object while you are attempting to compare it to adate
object.Solution: You can fix this error by changing it to
tx_uk.InvoiceDate.dt.date < date(2011,6,1)