Python Forum
How does pd.date_range() fit in with axs[].set_xticks() ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How does pd.date_range() fit in with axs[].set_xticks() ?
#1
My program works at the moment, but I'm trying to better understand why.

These three lines define x-axis tick labels for axs[0] subplot:

xtick_labels = pd.date_range(btstats['Date'].iloc[0],btstats['Date'].iloc[-1],20)  
xtick_labels_converted = xtick_labels.strftime('%Y-%m-%d') 
axs[0].set_xticks(list(np.linspace(1,len(btstats.index),num=len(xtick_labels_converted))),xtick_labels_converted, rotation=45)
The first line gets date range from first and last value of 'Date' column.

The second line is necessary to avoid "ConversionError: Failed to convert value(s) to axis units."

ax.set_xticks() arguments are list of tick locations, list of tick labels, and rotation amount.

Isolated from anything else (e.g. not part of a dataframe), what does it mean to have a DatetimeIndex? Is that just a type of object like a range object or generator object?

If I print(type()), then for lines 1 and 2, respectively, I get <class 'pandas.core.indexes.datetimes.DatetimeIndex'> and <class 'pandas.core.indexes.base.Index'>.

Why is the first object type not acceptable to line 3 (I'm guessing that's what causes the ConversionError) while the second is?
Reply
#2
set_xticks is expecting a list of str
Quote:matplotlib.axes.Axes.set_xticks
Axes.set_xticks(ticks, labels=None, *, minor=False, **kwargs)[source]
Set the xaxis' tick locations and optionally labels.

If necessary, the view limits of the Axis are expanded so that all given ticks are visible.

Parameters
tickslist of floats
List of tick locations.

labelslist of str, optional
List of tick labels. If not set, the labels show the data value.

minorbool, default: False
If False, set the major ticks; if True, the minor ticks.

**kwargs
Text properties for the labels. These take effect only if you pass labels. In other cases, please use tick_params.
Pandas date_range returns a Datetimeindex. A Datetimeindex is an immutable ndarray-like of datetime64 data
Quote:pandas.DatetimeIndex
class pandas.DatetimeIndex(data=None, freq=NoDefault.no_default, tz=None, normalize=False, closed=None, ambiguous='raise', dayfirst=False, yearfirst=False, dtype=None, copy=False, name=None)[source]
Immutable ndarray-like of datetime64 data.

Represented internally as int64, and which can be boxed to Timestamp objects that are subclasses of datetime and carry metadata.
So when you call pandas.daterange() you are getting back an array of things that look like 64 bit integers, not strings. Do you want a bunch of really big integers appearing as tick labels?
Mark17 likes this post
Reply
#3
(Jun-09-2022, 02:58 PM)deanhystad Wrote: ...
So when you call pandas.daterange() you are getting back an array of things that look like 64 bit integers, not strings. Do you want a bunch of really big integers appearing as tick labels?

Definitely not. I'm happy with the way this turns out. I just didn't quite understand how it's working. This helps. Thanks!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020