I have a data set with one key dimension and one value dimension, where the key dimension is month name and the value dimension is a number. When I plot this using a hv.Curve(...)
the months are not in chronological order. What is the best way to fix this?
Asked
Active
Viewed 372 times
2

rindis
- 869
- 11
- 25
-
Hi Martin, can you still add a small sample of your data / code? I've now just guessed in my answer that your date fields are just strings. – Sander van den Oord Nov 14 '19 at 08:51
1 Answers
1
My guess is your months are probably strings right now.
So easiest thing is to:
1. first convert your string months to proper pandas datetime field
2. and then sort your dataframe on the new datetime field
Here's an example:
# import libraries
import pandas as pd
import holoviews as hv
hv.extension('bokeh', logo=False)
# create sample dataframe
data = {
'month': ['August', 'June', 'May', 'April', 'July'],
'value': [3, 5, 7, 9, 5],
}
df = pd.DataFrame(data=data)
# convert month string to pandas datetime
# here I've added 2019 to the string, to make it a proper 2019 year
# but this isn't really necessary.
df['month2'] = pd.to_datetime('2019 ' + df['month'], format='%Y %B')
# sort your dataframe on the datetime field
df = df.sort_values(by='month2')
# since your dataframe is now sorted correctly, you can make your plot
hv.Curve(data=df, kdims='month', vdims='value')

Sander van den Oord
- 10,986
- 5
- 51
- 96
-
I would prefer if there was a way to do this without having to touch the dataframe – rindis Nov 18 '19 at 04:03
-
Here's some answers on custom sorting in pandas, maybe it helps: https://stackoverflow.com/questions/13838405/custom-sorting-in-pandas-dataframe – Sander van den Oord Nov 18 '19 at 10:04