2

I am trying to scatter plot some data from a csv file in Pandas, but I get an error.

The input file looks like this:

time,SPOT
2016-02-04 16:01:10.785000,3.6729
2016-02-04 16:01:11,4.2344

And the code I am using to plot:

import pandas as pd
df = pd.read_csv('file.csv')
df.plot(kind='scatter', x='time', y='SPOT')

I then get this error:

    Traceback (most recent call last):
      File "C:\installed\Python27\lib\site-packages\IPython\core\interactiveshell.py", line 3066, in run_code
        exec(code_obj, self.user_global_ns, self.user_ns)
      File "<ipython-input-51-ff49e6461746>", line 1, in <module>
        df.plot(kind='scatter', x='time', y='SPOT')
      File "C:\installed\Python27\lib\site-packages\pandas\tools\plotting.py", line 2477, in plot_frame
        **kwds)
      File "C:\installed\Python27\lib\site-packages\pandas\tools\plotting.py", line 2317, in _plot
        plot_obj.generate()
      File "C:\installed\Python27\lib\site-packages\pandas\tools\plotting.py", line 923, in generate
        self._make_plot()
      File "C:\installed\Python27\lib\site-packages\pandas\tools\plotting.py", line 1445, in _make_plot
        scatter = ax.scatter(data[x].values, data[y].values, c=c_values,
      File "C:\installed\Python27\lib\site-packages\pandas\core\frame.py", line 1780, in __getitem__
        return self._getitem_column(key)
      File "C:\installed\Python27\lib\site-packages\pandas\core\frame.py", line 1787, in _getitem_column
        return self._get_item_cache(key)
      File "C:\installed\Python27\lib\site-packages\pandas\core\generic.py", line 1068, in _get_item_cache
        values = self._data.get(item)
      File "C:\installed\Python27\lib\site-packages\pandas\core\internals.py", line 2849, in get
        loc = self.items.get_loc(item)
      File "C:\installed\Python27\lib\site-packages\pandas\core\index.py", line 1402, in get_loc
        return self._engine.get_loc(_values_from_object(key))
      File "pandas\index.pyx", line 134, in pandas.index.IndexEngine.get_loc (pandas\index.c:3807)
      File "pandas\index.pyx", line 154, in pandas.index.IndexEngine.get_loc (pandas\index.c:3687)
      File "pandas\hashtable.pyx", line 696, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12310)
      File "pandas\hashtable.pyx", line 704, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12261)
KeyError: 'time'

Q1. I am wondering if my time series are not allowed to contain milliseconds in the time column?

Q2. Sometimes the times in column two have no milliseconds, presumable when it is zero. I tried to add a date_parser but it does not help.

date_parser = lambda x: pd.datetime.strptime(x, '%Y-%m-%d %H:%M:%S.%f')
df = pd.read_csv('file.csv', date_parser=date_parser)

I am using this python version 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)]'

and pandas 0.15.2

2
  • Scatter doesn't work with datetime values. See github.com/pydata/pandas/issues/8113. Commented Feb 9, 2016 at 17:33
  • Thank you. Is there some kind of workaround? For example convert dates to a double? Commented Feb 9, 2016 at 17:38

1 Answer 1

3

The scatter plot doesn't work with datetime values. But, you can work around this, by doing a regular line graph and setting the style to .

df.plot(x='time', y='SPOT', style='.')

Mocking up some more data, I get a plot like this (you'll need to mess with the label styles, but you can get the idea of how it plots):

Sample work around

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.