KeyError: '[datetime.time(12, 19, 29) not in index'

python keyerror exception
keyerror pandas
how to avoid keyerror in python
python keyerror when key exists
keyerror 1 pandas
keyerror python json
keyerror: 'django
keyerror: 0 python

data is in epoch nanoseconds as below the sample shows:

time               duration
1.530706769E+018    40000000000
1.53070683E+018     41000000000
1.530706891E+018    41000000000
1.530706951E+018    41000000000
1.530707011E+018    41000000000
1.530707068E+018    37000000000
1.530707128E+018    41000000000
1.530707188E+018    41000000000
1.530707248E+018    40000000000
1.530707313E+018    45000000000
1.530707368E+018    40000000000
1.530707432E+018    44000000000
1.530707492E+018    44000000000
1.530707552E+018    44000000000
1.530707612E+018    39000000000
1.530707673E+018    41000000000
1.530707729E+018    37000000000
1.530712952E+018    45000000000
1.530713007E+018    40000000000
1.530713067E+018    35000000000
1.530713132E+018    40000000000
1.530713187E+018    36000000000
1.530713248E+018    36000000000
1.530713308E+018    40000000000
1.530713369E+018    41000000000
1.530713429E+018    40000000000
1.53071349E+018     41000000000
1.530713551E+018    42000000000
1.530713611E+018    40000000000
1.530713671E+018    40000000000
1.530713732E+018    40000000000
1.530713792E+018    39000000000
1.530713852E+018    39000000000
1.530713913E+018    40000000000
1.530713973E+018    40000000000
1.530714029E+018    36000000000
1.53071409E+018     36000000000
1.530714151E+018    41000000000
1.530714212E+018    41000000000
1.530714272E+018    41000000000
1.530714332E+018    40000000000
1.530714393E+018    41000000000
1.530714454E+018    41000000000
1.530714515E+018    42000000000
1.530714574E+018    39000000000
1.53071463E+018     39000000000
1.530714694E+018    44000000000
1.530714753E+018    40000000000
1.530714814E+018    41000000000
1.530714874E+018    42000000000
1.530714934E+018    41000000000
1.530714995E+018    41000000000
1.530715056E+018    41000000000
1.530715112E+018    37000000000
1.530715245E+018    16000000000
1.530715275E+018    15000000000
1.530715305E+018    15000000000
1.530715335E+018    15000000000
1.530715366E+018    16000000000
1.530715396E+018    15000000000
1.530715427E+018    16000000000
1.530715456E+018    15000000000
1.530715487E+018    16000000000
1.530715517E+018    15000000000
1.530715548E+018    16000000000
1.530715578E+018    15000000000

To transform I used code in pandas as:

import pandas as pd
import matplotlib.pyplot as plt 
import datetime from datetime

df = pd.read_csv('d1.csv')

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

df['Time'] = df['time'].dt.time

Above codes transforms data as in form:

              time             d      Time
0  2018-07-04 12:19:29  4.000000e+10  12:19:29
1  2018-07-04 12:20:30  4.100000e+10  12:20:30
2  2018-07-04 12:21:31  4.100000e+10  12:21:31
3  2018-07-04 12:22:31  4.100000e+10  12:22:31
4  2018-07-04 12:23:31  4.100000e+10  12:23:31
5  2018-07-04 12:24:28  3.700000e+10  12:24:28
6  2018-07-04 12:25:28  4.100000e+10  12:25:28
7  2018-07-04 12:26:28  4.100000e+10  12:26:28
8  2018-07-04 12:27:28  4.000000e+10  12:27:28
9  2018-07-04 12:28:33  4.500000e+10  12:28:33
10 2018-07-04 12:29:28  4.000000e+10  12:29:28
11 2018-07-04 12:30:32  4.400000e+10  12:30:32
12 2018-07-04 12:31:32  4.400000e+10  12:31:32
13 2018-07-04 12:32:32  4.400000e+10  12:32:32
14 2018-07-04 12:33:32  3.900000e+10  12:33:32
15 2018-07-04 12:34:33  4.100000e+10  12:34:33
16 2018-07-04 12:35:29  3.700000e+10  12:35:29
17 2018-07-04 14:02:32  4.500000e+10  14:02:32
18 2018-07-04 14:03:27  4.000000e+10  14:03:27
19 2018-07-04 14:04:27  3.500000e+10  14:04:27

Now I need a scatter plot between Time and d So I coded as:

x = df['Time']

y = df['d']

df.plot(x,y, 'scatter')

But got error as:

KeyError: '[datetime.time(12, 19, 29) datetime.time(12, 20, 30)\n datetime.time(12, 21, 31) ... datetime.time(23, 54, 39)\n datetime.time(23, 55, 16) datetime.time(23, 55, 52)] not in index'

What this above error means... How to resolve this and get a scatter plot here ?

You could use this:

df.plot(x='Time', y='d', style=".")

Python KeyError Exceptions and How to Handle Them – Real Python, The Python KeyError is a type of LookupError exception and denotes that there was an issue retrieving the key you were looking for. When you see a KeyError , the  Even more handy is somewhat controversially-named setdefault(key, val) which sets the value of the key only if it is not already in the dict, and returns that value in any case:

set time as index and plot it

df = df.set_index('time')

How to Handle a Python KeyError – Real Python, The ultimate goal is to stop unexpected KeyError exceptions from being raised. The usual solution is to use .get() : # ages =  When you encounter a KeyError, there are a few standard ways to handle it. Depending on your use case, some of these solutions might be better than others. The ultimate goal is to stop unexpected KeyError exceptions from being raised. The usual solution is to use.get() :

pandas.DataFrame.plot.scatter — pandas 0.23.4 documentation:

x : int or str

The column name or column position to be used as horizontal coordinates for each point.

y : int or str

The column name or column position to be used as vertical coordinates for each point.

So, it should be


I'm getting Key error in python, A KeyError generally means the key doesn't exist. So, are you sure the path key exists? From the official python docs: exception KeyError. A KeyError is avoidable in Python programs. We fix the problem by using a safe method, such as get(), instead of directly accessing the key. We can only directly access a key if we are sure it exists. Also: If any direct accesses occur in your program, using a try-except block may be worthwhile if your code is new or untested.

Try matplotlib

import matplotlib.pyplot as plt

plt.plot(df['Time'], df['d'])

What is the Python keyerror?, The keyerror occurs when the key used for a lookup does not exist in the mapping. Consider a phone book that is represented as a dictionary in Python. In the following example, a small dictionary with only three items is created. The dictionary stores dummy phone numbers with names. The names act as the key while phone numbers as values. As you run the program, it will ask to enter the name. If the name (key) exists in the dictionary, it will display the phone number, otherwise, except block

Python Exception Handling - KeyError, A deep look at the KeyError in Python, with code samples illustrating the basic use of dictionary, and how invalid keys can raise KeyErrors. Python Exception Handling – KeyError January 17, 2018 Andrew Powell-Morse in python, Python Exception Handling Today, as we make our way through our detailed Python Exception Handling series, we’ll be looking into the KeyError, which is the close sibling of the IndexError we looked at last week.

Python KeyError Fix: Use Dictionary get, KeyError. A dictionary can cause a KeyError to occur. This happens due to incorrect use of the dictionary. We can prevent the KeyError in most cases by using  KeyError: [key] error occurs in Python when you are trying to access a key that does not exist in the dictionary. It can be fixed using an exception handler or alternatively using an if condition to check for the key beforehand.

What is Python KeyError and How to Handle it with 3 Examples, The KeyError is raised when you try to access a key in a dictionary that does not exist. This tutorial addresses how to deal with KeyError. I have a text file containing the Conditional Probability Tables for each node in the Bayesian Network. There are two new lines between every pair of node description. An example of such a text fil

  • Even without that error, you won't be able to use a scatter plot here because your x-axis isn't numeric
  • @user3483203 Can't x-axis have a value in %HH:%MM:%SS which is a datetime object and y axis with a 'float' value ??
  • Worked crisply Thanks @Joe
  • df.plot('Time','d','scatter') gives a ValueError: scatter requires x column to be numeric.
  • @suryarahul Yes, I didn't look any further. That is a problem unrelated to what you asked -- KeyError: '[datetime.time(12, 19, 29) not in index'.
  • @suryarahul As has been noted on your post itself, Times cannot be used as the x-axis ... which is exactly what this error is telling you.