amueller / introduction_to_ml_with_python

Notebooks and code for the book "Introduction to Machine Learning with Python"
7.45k stars 4.57k forks source link

TypeError: Cannot compare type 'Timestamp' with type 'float' [for Fig.4-12, In[50], page-245] #54

Closed GaussEuler closed 6 years ago

GaussEuler commented 6 years ago

Respected Dr. Muller, please help me with this error. Running your code as stated exactly in the book in Chapter 4, In[50] (p-245) gives me the following error: TypeError: Cannot compare type 'Timestamp' with type 'float'

The code is as follows: import matplotlib.pyplot as plt import pandas as pd import mglearn

citibike = mglearn.datasets.load_citibike()

plt.figure(figsize=(10, 3)) xticks = pd.date_range(start=citibike.index.min(), end=citibike.index.max(), freq='D') plt.xticks(xticks, xticks.strftime("%a %m-%d"), rotation=90, ha="left") plt.plot(citibike, linewidth=1) plt.xlabel("Date") plt.ylabel("Rentals")

I am running this code in (Anaconda) Spyder, version 3.2.4 and Pandas version is 0.21.0

Thank you, Sincerely, Nikhilesh

amueller commented 6 years ago

Hey. Sorry you're having issues and thanks for reaching out. What's citibike.type? Also, can you please provide the full traceback?

amueller commented 6 years ago

I can reproduce, seems like an incompatible change in pandas 0.21.0

amueller commented 6 years ago

This workaround seems to work:

plt.xticks(xticks.astype("int"), xticks.strftime("%a %m-%d"), rotation=90, ha="left")

note the .astype("int")

GaussEuler commented 6 years ago

Thank you so much Dr. Muller. Yes, the workaround code works perfectly! Again, thanks a lot for such a quick response. I sincerely appreciate. (I was trying with astype('float') but it was not working)

GaussEuler commented 6 years ago

citibike.type Traceback (most recent call last):

File "", line 1, in citibike.type

File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py", line 3614, in getattr return object.getattribute(self, name)

AttributeError: 'Series' object has no attribute 'type'

GaussEuler commented 6 years ago

And herein is the full Traceback of the initial problem Traceback (most recent call last):

File "", line 1, in runfile('C:/Users/Numerical Nikhilesh/.spyder-py3/temp.py', wdir='C:/Users/Numerical Nikhilesh/.spyder-py3')

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 710, in runfile execfile(filename, namespace)

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 101, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/Numerical Nikhilesh/.spyder-py3/temp.py", line 26, in plt.xticks(xticks, xticks.strftime("%a %m-%d"), rotation=90, ha="left")

File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\pyplot.py", line 1618, in xticks locs = ax.set_xticks(args[0])

File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\axes_base.py", line 3003, in set_xticks ret = self.xaxis.set_ticks(ticks, minor=minor)

File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\axis.py", line 1647, in set_ticks self.set_view_interval(min(ticks), max(ticks))

File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\axis.py", line 1996, in set_view_interval self.axes.viewLim.intervalx = (min(vmin, vmax, Vmin),

File "pandas/_libs/tslib.pyx", line 1166, in pandas._libs.tslib._Timestamp.richcmp

TypeError: Cannot compare type 'Timestamp' with type 'float'

amueller commented 6 years ago

Sorry, I had meant .dtype but yeah, this is http://pandas-docs.github.io/pandas-docs-travis/whatsnew.html#no-automatic-matplotlib-converters

GaussEuler commented 6 years ago

Thank you for providing that link. Yes, with the help of converter, one then can use your code from the book exactly as it is without adding .astype("int") Thank you again sir.

amueller commented 6 years ago

Will be fixed in the next print, I fixed it in this repo, too.