Search code examples
pythonpandaspie-chartepoch

unable to plot a pie chart


I have a dataset in epoch nano seconds

       M            d          time      
0  1081083  28000000000  1.530683e+18  
1  1081083  16000000000  1.530683e+18  
2  1081085  33000000000  1.530683e+18  
3  1081083  28000000000  1.530683e+18  
4  1081085  27000000000  1.530683e+18

which on conversion looks like this:

      M         d           time
0  1081083  07:16:40 2018-07-04 05:42:20  
1  1081083  09:56:40 2018-07-04 05:43:03  
2  1081085  16:10:00 2018-07-04 05:43:12  
3  1081083  07:16:40 2018-07-04 05:43:51  
4  1081085  05:30:00 2018-07-04 05:44:01

For conversion of epoch to normal the codes are:

import pandas as pd
import time
import matplotlib.pyplot as plt


df1 = pd.read_csv('testsy_1.csv')

df1['time']=pd.to_datetime(df1['time'], unit='ns')

df1['d']=df1['d'].apply(lambda x: time.strftime("%H:%M:%S",time.localtime(x)))

But when trying get a pie-chart for df1['M'],df1['d'] :

plt.figure(figsize=(16,8))
ax1 = plt.subplot(121, aspect='equal')
df1.plot(kind='pie', y = 'd', ax=ax1, autopct='%1.1f%%', 
startangle=90, shadow=False, labels=df1['M'], legend = False, fontsize=14)

I get a error as :

TypeError: Empty 'DataFrame': no numeric data to plot

How dataframe is empty as converted data is already there ? How to plot here the pie chart ?

As suggested by @jezrael I omitted df1['d']=df1['d'].apply(lambda x: time.strftime("%H:%M:%S",time.localtime(x))) and executed the script without any change fetch me the results for the df.head() of the dataset.

But when applying the above this for a full dataset of about 23000 rows, I get a horrible plot...What is the problem?

enter image description here


Solution

  • There is problem d values are not numeric.

    So you can convert d column to timedeltas and then to seconds:

    df1['d'] = pd.to_timedelta(df1['d']).dt.total_seconds()
    print (df1)
             M        d                time
    0  1081083  26200.0 2018-07-04 05:42:20
    1  1081083  35800.0 2018-07-04 05:43:03
    2  1081085  58200.0 2018-07-04 05:43:12
    3  1081083  26200.0 2018-07-04 05:43:51
    4  1081085  19800.0 2018-07-04 05:44:01
    

    pic

    Or if possible omit:

    df1['d']=df1['d'].apply(lambda x: time.strftime("%H:%M:%S",time.localtime(x)))
    

    graph