Jack
Jack

Reputation: 722

Stacked Barplot with datetime64[ns] values. Using the first column as starting point

I have a dataset of runners going through checkpoints it looks like this:

data = {'Runner': ['Tom', 'Joseph', 'Krish'], 
'Start': [30/04/2021 05:27:19, 30/04/2021 05:33:50, 30/04/2021 05:43:32],
'First Checkpoint': [30/04/2021 05:40:45, 30/04/2021 05:36:59, 30/04/2021 05:59:03],
'Second Checkpoint': [30/04/2021 05:42:50, 30/04/2021 05:42:31, 30/04/2021 06:01:19],
'Third Checkpoint': [30/04/2021 05:42:53, 30/04/2021 05:42:33, 30/04/2021 06:01:37]}  

df = pd.DataFrame(data)

I would like to visualize this data as a stacked barplot but using the Start column as the first ticker in the graph.

My problems are that datetime64[ns] is not supported by matplotlib and I can't find a workaround to even start plotting.

Any help on this?

Upvotes: 0

Views: 24

Answers (1)

imdevskp
imdevskp

Reputation: 2233

You need to convert data to proper DateTime format using pd.to_datetime

data = {'Runner': ['Tom', 'Joseph', 'Krish'], 
        'Start': ['30/04/2021 05:27:19', '30/04/2021 05:33:50', '30/04/2021 05:43:32'],
        'First Checkpoint': ['30/04/2021 05:40:45', '30/04/2021 05:36:59', '30/04/2021 05:59:03'],
        'Second Checkpoint': ['30/04/2021 05:42:50', '30/04/2021 05:42:31', '30/04/2021 06:01:19'],
        'Third Checkpoint': ['30/04/2021 05:42:53', '30/04/2021 05:42:33', '30/04/2021 06:01:37']}  

df = pd.DataFrame(data)

for col in df.columns[1:]:
    df[col] = pd.to_datetime(df[col])
    
df.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 5 columns):
 #   Column             Non-Null Count  Dtype         
---  ------             --------------  -----         
 0   Runner             3 non-null      object        
 1   Start              3 non-null      datetime64[ns]
 2   First Checkpoint   3 non-null      datetime64[ns]
 3   Second Checkpoint  3 non-null      datetime64[ns]
 4   Third Checkpoint   3 non-null      datetime64[ns]
dtypes: datetime64[ns](4), object(1)
memory usage: 248.0+ bytes

Upvotes: 0

Related Questions