surya rahul
surya rahul

Reputation: 883

how to convert datetime to numeric data type?

I have a dataset as

time               MachineId  
1530677359000000000 01081081  
1530677363000000000 01081081  
1530681023000000000 01081090  
1530681053000000000 01081090  
1530681531000000000 01081090

So my codes goes like:

import pandas as pd
from datetime import datetime
import time
import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdate

df= pd.read_csv('acn.csv')`

df['time']=pd.to_datetime(df['time'], unit='ns')` #converting the epoch nanosec time to datetime-format

print(df.head())

Output:

   time            MachineId   
0 2018-07-04 04:09:19  1081081.0  
1 2018-07-04 04:09:23  1081081.0  
2 2018-07-04 05:10:23  1081090.0   
3 2018-07-04 05:10:53  1081090.0  
4 2018-07-04 05:18:51  1081090.0 

and now I want to change my data of time to numeric to generate a plot between time and machine id

dates = plt.dates.date2num(df['time'])
df.plot(kind='scatter',x='dates',y='MachineId')
plt.show()

which throws a error as :

AttributeError: 'module' object has no attribute 'dates'

How can I change datetime format to numeric so that a plot can be formed ?

Upvotes: 1

Views: 3826

Answers (2)

Jeff Ellen
Jeff Ellen

Reputation: 552

You got the following error:

AttributeError: 'module' object has no attribute 'dates'

Your error message is telling you that matplotlib.pyplot.dates (plt.dates) doesn't exist. (The error says that there's a module that you're calling 'dates' but it doesn't exist).

So you need to fix that error before you worry about converting anything. Did you mean to call matplotlib.dates.date2num instead? In your code you have the following:

import matplotlib.dates as mdate

So maybe you meant to call mdate.date2num instead? That should eliminate the AttributeError.

If that doesn't work for you, you could try what is suggested in the link provided by one of the other commenters, to use pandas to_pydatetime. I'm not familiar with it, but in this example page, it is accessed as Series.dt.to_pydatetime()

All of this converting is just necessary because you are trying to use df.plot; maybe you should consider just calling matplotlib directly. For example, could you just use plt.plot_date instead? (here's the link to it). Pandas is excellent, but the plotting interface isn't as mature as the rest of it. As an example (I'm not saying this is the exact problem you are having) but here is a known bug in pandas regarding plotting dates. Here is an older stack overflow thread where someone stubs out a plt.plot_date method for you.

Upvotes: 1

Nikolas Pitsillos
Nikolas Pitsillos

Reputation: 61

You can directly plot dates as well. For example if you want to have the date on the x-axis you pass the dates in ax.plot(df.time, ids). I think this might the closest thing to what you look for.

Upvotes: 0

Related Questions