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


Messages In This Thread
How does pd.date_range() fit in with axs[].set_xticks() ? - by Mark17 - Jun-09-2022, 02:25 PM

Forum Jump:

User Panel Messages

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