Python error on data.plot(x=data.timestamp, style=".-") - python

In Python I am trying to use the following command to get timestamp info on my plot. I've imported the proper packages pandas and pylab. The data is all cleaned as well.
data.plot(x=data.timestamp, style=".-")
I keep getting a massive error with lots of different things. I am following along to https://www.youtube.com/watch?v=5XGycFIe8qE and it comes at 38 minutes. Here is the error I get: It's massive
Traceback (most recent call last): File "", line 1, in data.plot(x=data.timestamp, style=".-") File "C:\Python3\lib\site-packages\pandas\plotting_core.py", line 2673, in call sort_columns=sort_columns, **kwds) File "C:\Python3\lib\site-packages\pandas\plotting_core.py", line 1900, in plot_frame **kwds) File "C:\Python3\lib\site-packages\pandas\plotting_core.py", line 1727, in _plot plot_obj.generate() File "C:\Python3\lib\site-packages\pandas\plotting_core.py", line 260, in generate self._post_plot_logic_common(ax, self.data) File "C:\Python3\lib\site-packages\pandas\plotting_core.py", line 395, in _post_plot_logic_common self._apply_axis_properties(ax.yaxis, fontsize=self.fontsize) File "C:\Python3\lib\site-packages\pandas\plotting_core.py", line 468, in _apply_axis_properties labels = axis.get_majorticklabels() + axis.get_minorticklabels() File "C:\Python3\lib\site-packages\matplotlib\axis.py", line 1188, in get_majorticklabels ticks = self.get_major_ticks() File "C:\Python3\lib\site-packages\matplotlib\axis.py", line 1339, in get_major_ticks numticks = len(self.get_major_locator()()) File "C:\Python3\lib\site-packages\matplotlib\dates.py", line 1054, in call self.refresh() File "C:\Python3\lib\site-packages\matplotlib\dates.py", line 1074, in refresh dmin, dmax = self.viewlim_to_dt() File "C:\Python3\lib\site-packages\matplotlib\dates.py", line 832, in viewlim_to_dt return num2date(vmin, self.tz), num2date(vmax, self.tz) File "C:\Python3\lib\site-packages\matplotlib\dates.py", line 441, in num2date return _from_ordinalf(x, tz) File "C:\Python3\lib\site-packages\matplotlib\dates.py", line 256, in _from_ordinalf dt = datetime.datetime.fromordinal(ix).replace(tzinfo=UTC) ValueError: ordinal must be >= 1

I had a similar issue and I believe it is a version issue with pandas and the Jupyter Notebook that are different from what Quentin is using in his examples here: https://github.com/QCaudron/pydata_pandas and what you, and I, are using.
Try this:
data.plot('timestamp', style='.-')
or
data.plot(x='timestamp', style='.-')
Per the pandas docs https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.plot.html either a label or position should work.
DataFrame
x : label or position, default None
y : label or position, default None
Allows plotting of one column versus another

Related

Error merging multiple CSV files - Python

I'm trying to merge several CSV files into one.
Searching several methods, I found this one:
files = glob.glob("D:\\green_lake\\Projects\\covid_19\\tabelas_relacao\\acre\\*.csv")
files_merged = pd.concat([pd.read_csv(df) for df in files], ignore_index=True)
When running this error is returned:
>>> files_merged = pd.concat([pd.read_csv(df) for df in files], ignore_index=True)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <listcomp>
File "C:\Users\Leonardo\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\util\_decorators.py", line 311, in wrapper
return func(*args, **kwargs)
File "C:\Users\Leonardo\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py", line 678, in read_csv
return _read(filepath_or_buffer, kwds)
File "C:\Users\Leonardo\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py", line 581, in _read
return parser.read(nrows)
File "C:\Users\Leonardo\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py", line 1253, in read
index, columns, col_dict = self._engine.read(nrows)
File "C:\Users\Leonardo\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\c_parser_wrapper.py", line 225, in
read
chunks = self._reader.read_low_memory(nrows)
File "pandas\_libs\parsers.pyx", line 805, in pandas._libs.parsers.TextReader.read_low_memory
File "pandas\_libs\parsers.pyx", line 861, in pandas._libs.parsers.TextReader._read_rows
File "pandas\_libs\parsers.pyx", line 847, in pandas._libs.parsers.TextReader._tokenize_rows
File "pandas\_libs\parsers.pyx", line 1960, in pandas._libs.parsers.raise_parser_error
pandas.errors.ParserError: Error tokenizing data. C error: Expected 1 fields in line 243, saw 4
I'm starting to study python and if it's a stupid mistake, I apologize ;)

Python- not in index error (one-hot encoded)

I'm working on the Titanic dataset and one-hot applied to all columns. So everything 0 and 1.
However, I get the following error in train operation. I tried many things but I could not find the solution. I tried to explain the error as much as I can, I hope you can understand the problem easily. Thank you for your time.
Traceback (most recent call last):
File "titanic_classifier.py", line 277, in <module>
fire.Fire()
File "C:\Users\PC\Anaconda3\lib\site-packages\fire\core.py", line 138, in Fire
component_trace = _Fire(component, args, parsed_flag_args, context, name)
File "C:\Users\PC\Anaconda3\lib\site-packages\fire\core.py", line 468, in _Fire
target=component.__name__)
File "C:\Users\PC\Anaconda3\lib\site-packages\fire\core.py", line 672, in _CallAndUpdateTrace
component = fn(*varargs, **kwargs)
File "titanic_classifier.py", line 220, in train
n_sample = nsample if nsample is not None else m['n_sample'])
File "C:\Users\PC\Desktop\x\source\run_train.py", line 248, in run_train
dfXy, dfXytest = train(model_dict, dfXy, cols, post_process_fun)
File "C:\Users\PC\Desktop\x\source\run_train.py", line 125, in train
data_pars['train'] = {'Xtrain' : dfX[colsX].iloc[:itrain, :],
File "C:\Users\PC\Anaconda3\lib\site-packages\pandas\core\frame.py", line 2908, in __getitem__
indexer = self.loc._get_listlike_indexer(key, axis=1, raise_missing=True)[1]
File "C:\Users\PC\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 1254, in _get_listlike_indexer
self._validate_read_indexer(keyarr, indexer, axis, raise_missing=raise_missing)
File "C:\Users\PC\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 1304, in _validate_read_indexer
raise KeyError(f"{not_found} not in index")
KeyError: "['space_svd_0', 'interaction_svd_1', 'transit_svd_0', 'summary_svd_1', 'last_scraped_month', 'neighborhood_overview_svd_0', 'host_name_svd_0', 'house_rules_svd_1', 'house_rules_svd_0', 'first_review_month', 'transit_svd_1', 'last_review_year', 'interaction_svd_0', 'first_review_day', 'last_scraped_day', 'summary_svd_0', 'access_svd_1', 'first_review_year', 'notes_svd_0', 'name_svd_1', 'space_svd_1', 'neighborhood_overview_svd_1', 'host_since_year', 'amenities_svd_1', 'amenities_svd_0', 'host_about_svd_0', 'host_name_svd_1', 'notes_svd_1', 'name_svd_0', 'last_review_day', 'last_scraped_year', 'description_svd_0', 'host_since_day', 'last_review_month', 'access_svd_0', 'host_about_svd_1', 'host_since_month', 'description_svd_1'] not in index"
The part of the code where the error came from run_train.py:
data_pars['train'] = {'Xtrain' : dfX[colsX].iloc[:itrain, :],
'ytrain' : dfX[coly].iloc[:itrain],
'Xtest' : dfX[colsX].iloc[itrain:ival, :],
'ytest' : dfX[coly].iloc[itrain:ival],
'Xval' : dfX[colsX].iloc[ival:, :],
'yval' : dfX[coly].iloc[ival:]
}

Creating a histogram and understanding both maplotlib axes and subplot

I've just had some trouble piecing together matplotlib documentation particularly for my data set. I think I have a small enough block of code that shows all of my lack of understanding despite reading documentation. The documentation i've beeen trying to use for reference was initially this for creating a line graph https://matplotlib.org/gallery/text_labels_and_annotations/date.html
I've been trying to plot a numpy array, post_records containing two columns. I'm working with social media data, so the first column is for post_ids and the second is for datetime_obj_col that I managed to read from csv file using some scripting.
I managed to create a line graph with this data in matplotlib, but I don't quite know how to make a histogram.
Right now, nothing shows up when I run my program
fig, ax = plt.subplots()
hist, bins, patch_lst = ax.hist(post_records[:,1], bins=range(31)) # thought that bins could be a sequence, wanted to create 31 bins for 31 total days in a month
ax.plot(hist, bins)
ax.set_xlabel('Days')
ax.set_ylabel('frequency')
ax.set_title(r'Histogram of Time')
plt.show() # shows nothing
What do I need to pass to ax.plot? I'm unclear about how to pass in my x dataset
why isn't the window showing?
Edit with how to replicate this:
def create_dataframe_of_datetime_objects_and_visualize():
datetime_lst = [1521071920000000000, 1521071901000000000, 1521071844000000000, 1521071741000000000, 1521071534000000000] # to get this variable I loaded my original dataframe with 1980000, sliced the first 5 entries, then printed out the 'datetime_obj_col'. I can't exactly remember what this format is called, I think it's unix time.
id_lst = [974013, 974072, 327212, 123890, 438201]
for each in range(len(datetime_lst)):
datetime_lst[each] = pd.to_datetime(datetime_lst[each], errors='coerce')
datetime_lst[each] = datetime_lst[each].strftime("%d-%b-%y %H:%M:%S")
datetime_lst[each] = pd.to_datetime(datetime_lst[each], errors='coerce', dayfirst=True, format="%d-%b-%y %H:%M:%S")
datetime_lst = pd.Series(datetime_lst)
df = pd.DataFrame({'tweet_id':id_lst, 'datetime_obj_col': datetime_lst})
gb_var = df.groupby(df["datetime_obj_col"].dt.month)
gb_var_count = gb_var.count()
gb_var.plot(kind="bar")
plt.show()
note that I am not using histogram anymore. But there should be two errors that come up, the following:
Traceback (most recent call last):
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\pandas\core
\groupby\groupby.py", line 918, in apply
result = self._python_apply_general(f)
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\pandas\core
\groupby\groupby.py", line 936, in _python_apply_general
self.axis)
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\pandas\core
\groupby\groupby.py", line 2273, in apply
res = f(group)
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\pandas\core
\groupby\groupby.py", line 541, in f
return self.plot(*args, **kwargs)
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\pandas\plot
ting_core.py", line 2941, in call
sort_columns=sort_columns, **kwds)
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\pandas\plot
ting_core.py", line 1977, in plot_frame
**kwds)
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\pandas\plot
ting_core.py", line 1804, in _plot
plot_obj.generate()
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\pandas\plot
ting_core.py", line 266, in generate
self._post_plot_logic_common(ax, self.data)
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\pandas\plot
ting_core.py", line 405, in _post_plot_logic_common
self._apply_axis_properties(ax.yaxis, fontsize=self.fontsize)
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\pandas\plot
ting_core.py", line 478, in _apply_axis_properties
labels = axis.get_majorticklabels() + axis.get_minorticklabels()
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\matplotlib\
axis.py", line 1245, in get_majorticklabels
ticks = self.get_major_ticks()
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\matplotlib\
axis.py", line 1396, in get_major_ticks
numticks = len(self.get_major_locator()())
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\matplotlib\
dates.py", line 1249, in call
self.refresh()
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\matplotlib\
dates.py", line 1269, in refresh
dmin, dmax = self.viewlim_to_dt()
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\matplotlib\
dates.py", line 1026, in viewlim_to_dt
.format(vmin))
ValueError: view limit minimum 0.0 is less than 1 and is an invalid Matplotlib d
ate value. This often happens if you pass a non-datetime value to an axis that h
as datetime units
Edit:
This is starting to seem like a bug related specifically to trying to use hist() to plot a column of datetime objects.
I took data from post_records which is a loaded numpy array that stores a 2d data set of 198000+ post ids and datetime objects.
This is the code for a function called create datetime objects. It opens a csv file "tweet_time_info_preprocessed.csv," which only has three columns: 'tweet_id" "tweet_created_at_date," and "tweet_created_at_hour." The following is code to combine the tweet_created_at_date and tweet_created_at_hour columns into formatted datetime objects using the pandas library to_datetime() method.
Csv file sample
def create_datetime_objects():
with open("post_time_info_preprocessed.csv", 'r', encoding='utf8') as time_csv:
mycsv = csv.reader(time_csv)
progress = 0
for row in mycsv:
progress +=1
if progress == 1: #header row
continue
if progress % 10000 == 0:
print(progress)
each_post_datetime_lst = []
each_post_datetime_lst.append(row[0])
time_str = str(row[1]) + " " + str(row[2])
a_date_object = pd.to_datetime(time_str, dayfirst=True, format="%d-%b-%y %H:%M:%S")
each_post_datetime_lst.append(a_date_object)
post_and_datetime_lst.append(each_tweet_datetime_lst)
numpy_arr_of_tweets_and_datetimes = np.array(tweets_and_datetime_objs)
np.save(np_save_path, numpy_arr_of_tweets_and_datetimes)
then I have visualize_objects_histogram()
def visualize_objects_histogram():
print("Visualizing timeplot as histogram")
post_records= np.load("tweets_and_datetime_objects.npy")
df = pd.DataFrame(data=post_records, columns=['post_id', 'datetime_obj_col'])
df_sliced = df[0:5]
print(df_sliced)
fig, ax = plt.subplots()
hist, bins, patch_lst = ax.hist(df_sliced['datetime_obj_col'], bins=range(5))
ax.plot(hist, bins)
ax.set_xlabel('Days')
ax.set_ylabel('frequency')
ax.set_title('Histogram of Time')
plt.show()
So I sliced off 5 rows of the data frame and stored them into df_slice. I run this code, a blank white window appears. Printing df_slice gives
tweet_id datetime_obj_col
0 974072352958042112 2018-03-14 23:58:40
1 974072272578166784 2018-03-14 23:58:21
2 974072032177598464 2018-03-14 23:57:24
3 974071601313533953 2018-03-14 23:55:41
4 974070732777914368 2018-03-14 23:52:14
And there's also an error message that comes with the blank white window. It's very long.
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\biney\AppData\Local\Programs\Python\Python36-32\lib\tkinter__i
nit__.py", line 1699, in call
return self.func(*args)
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\matplotlib\
backends_backend_tk.py", line 227, in resize
self.draw()
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\matplotlib\
backends\backend_tkagg.py", line 12, in draw
super(FigureCanvasTkAgg, self).draw()
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\matplotlib\
backends\backend_agg.py", line 433, in draw
self.figure.draw(self.renderer)
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\matplotlib\
artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\matplotlib\
figure.py", line 1475, in draw
renderer, self, artists, self.suppressComposite)
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\matplotlib\
image.py", line 141, in _draw_list_compositing_images
a.draw(renderer)
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\matplotlib\
artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\matplotlib\
axes_base.py", line 2607, in draw
mimage._draw_list_compositing_images(renderer, self, artists)
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\matplotlib\
image.py", line 141, in _draw_list_compositing_images
a.draw(renderer)
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\matplotlib\
artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\matplotlib\
axis.py", line 1190, in draw
ticks_to_draw = self._update_ticks(renderer)
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\matplotlib\
axis.py", line 1028, in _update_ticks
tick_tups = list(self.iter_ticks()) # iter_ticks calls the locator
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\matplotlib\
axis.py", line 971, in iter_ticks
majorLocs = self.major.locator()
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\matplotlib\
dates.py", line 1249, in call
self.refresh()
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\matplotlib\
dates.py", line 1269, in refresh
dmin, dmax = self.viewlim_to_dt()
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\matplotlib\
dates.py", line 1026, in viewlim_to_dt
.format(vmin))
ValueError: view limit minimum -0.19500000000000003 is less than 1 and is an inv
alid Matplotlib date value. This often happens if you pass a non-datetime value
to an axis that has datetime units
This error message repeats 5 times with slightly different values for the "view limit." Possibly 5 error messages ffor my 5 records. I think the error messages are most closely related to the following online version of dates.py... Could be wrong : https://fossies.org/linux/matplotlib/lib/matplotlib/dates.py (around line 1022, I'm going to check the actual file on my computer soon).
I'm going to try stuff from this post to see if it will help: Can Pandas plot a histogram of dates?
Edit 2:
The previous stackoverflow introduced me to two helpful methods, but they didn't work. I changed my visualize... function to the following
def visualize_datetime_objects_with_pandas():
tweets_and_datetime_objects = np.load("tweets_and_datetime_objects.npy") # contains python datetime objects
print("with pandas")
print(tweets_and_datetime_objects.shape)
df = pd.DataFrame(data=tweets_and_datetime_objects, columns=['tweet_id', 'datetimeobj'])
pandas_freq_dict = df['datetimeobj'].value_counts().to_dict()
#print(pandas_freq_dict)
print(len(list(pandas_freq_dict.keys())))
print(list(pandas_freq_dict.keys())[0])
print(list(pandas_freq_dict.values())[1])
plt.plot(pandas_freq_dict.keys(), pandas_freq_dict.values())
#df = df.set_index('datetimeobj')
# changing the index of this dataframe to a time index
#df['datetimeobj'].plot(kind='line', style=['--'])
plt.show()
It gives the following output/error message.
date-time temporal data visualization script
Visualizing timeplot as histogram
tweet_id datetime_obj_col
datetime_obj_col
14 5 5
tweet_id datetime_obj_col
datetime_obj_col
14 5 5
Traceback (most recent call last):
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\pandas\core
\groupby\groupby.py", line 918, in apply
result = self._python_apply_general(f)
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\pandas\core
\groupby\groupby.py", line 936, in _python_apply_general
self.axis)
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\pandas\core
\groupby\groupby.py", line 2273, in apply
res = f(group)
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\pandas\core
\groupby\groupby.py", line 541, in f
return self.plot(*args, **kwargs)
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\pandas\plot
ting_core.py", line 2941, in call
sort_columns=sort_columns, **kwds)
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\pandas\plot
ting_core.py", line 1977, in plot_frame
**kwds)
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\pandas\plot
ting_core.py", line 1804, in _plot
plot_obj.generate()
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\pandas\plot
ting_core.py", line 266, in generate
self._post_plot_logic_common(ax, self.data)
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\pandas\plot
ting_core.py", line 405, in _post_plot_logic_common
self._apply_axis_properties(ax.yaxis, fontsize=self.fontsize)
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\pandas\plot
ting_core.py", line 478, in _apply_axis_properties
labels = axis.get_majorticklabels() + axis.get_minorticklabels()
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\matplotlib\
axis.py", line 1245, in get_majorticklabels
ticks = self.get_major_ticks()
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\matplotlib\
axis.py", line 1396, in get_major_ticks
numticks = len(self.get_major_locator()())
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\matplotlib\
dates.py", line 1249, in call
self.refresh()
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\matplotlib\
dates.py", line 1269, in refresh
dmin, dmax = self.viewlim_to_dt()
File "C:\Users\biney\AppData\Roaming\Python\Python36\site-packages\matplotlib\
dates.py", line 1026, in viewlim_to_dt
.format(vmin))
ValueError: view limit minimum 0.0 is less than 1 and is an invalid Matplotlib d
ate value. This often happens if you pass a non-datetime value to an axis that h
as datetime units

to_csv error with pandas dataframe with timezone

The following code gives an error under the pandas 0.17 but work very well with the 0.16.2.
No problem with the to_pickle function but get an error with the to_csv.
Has someone a tip to deal with it ?
In[23]: new_index = pd.date_range('2015-01-01', '2015-12-31', freq = 'H', tz='Europe/Paris')
In[24]: df = pd.DataFrame({}, index = new_index)
In[25]: df['test'] = 1.
In[26]: df.to_pickle(r'test.h5')
In[27]: df.to_csv(r'test.csv')
Traceback (most recent call last):
File "C:\Anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 3035, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-27-2ced74ae66e1>", line 1, in <module>
df.to_csv(r'test.csv')
File "C:\Anaconda\lib\site-packages\pandas\core\frame.py", line 1289, in to_csv
formatter.save()
File "C:\Anaconda\lib\site-packages\pandas\core\format.py", line 1494, in save
self._save()
File "C:\Anaconda\lib\site-packages\pandas\core\format.py", line 1594, in _save
self._save_chunk(start_i, end_i)
File "C:\Anaconda\lib\site-packages\pandas\core\format.py", line 1619, in _save_chunk
quoting=self.quoting)
File "C:\Anaconda\lib\site-packages\pandas\core\index.py", line 1292, in to_native_types
return values._format_native_types(**kwargs)
File "C:\Anaconda\lib\site-packages\pandas\tseries\index.py", line 746, in _format_native_types
format = _get_format_datetime64_from_values(self, date_format)
File "C:\Anaconda\lib\site-packages\pandas\core\format.py", line 2191, in _get_format_datetime64_from_values
is_dates_only = _is_dates_only(values)
File "C:\Anaconda\lib\site-packages\pandas\core\format.py", line 2145, in _is_dates_only
values = DatetimeIndex(values)
File "C:\Anaconda\lib\site-packages\pandas\util\decorators.py", line 89, in wrapper
return func(*args, **kwargs)
File "C:\Anaconda\lib\site-packages\pandas\tseries\index.py", line 344, in __new__
ambiguous=ambiguous)
File "pandas\tslib.pyx", line 3753, in pandas.tslib.tz_localize_to_utc (pandas\tslib.c:64516)
AmbiguousTimeError: Cannot infer dst time from Timestamp('2015-10-25 02:00:00'), try using the 'ambiguous' argument
This seems to be a known bug #11619 and should be fixed in 0.17.1
The underlying issue is that your timeframe crosses from standard time to daylight saving time, which is the exact time showing in the error AmbiguousTimeError: Cannot infer dst time from Timestamp('2015-10-25 02:00:00')

How to get rid of maximum recursion depth error while plotting interactively?

I'm trying to build an interactive plot. This one is supposed to clear the figure if clicked within axes and draw a circle at a random place. The code is as follows:
import matplotlib.pyplot as plt
import random
def draw_circle(event):
if event.inaxes:
print(event.xdata, event.ydata)
plt.cla()
a = random.randint(0,100)
b = random.randint(0,100)
s, = plt.plot(a,b,'o', ms=100, color="blue",visible=True )
plt.show()
fig = plt.figure()
ax = plt.subplot(111)
s, = plt.plot(1,2,'o', ms=100, color="blue",visible=True )
plt.connect("button_press_event", draw_circle)
plt.show()
After clicking for 42 times, the program breaks and I get the following traceback:
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1413, in __call__
return self.func(*args)
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 286, in button_press_event
FigureCanvasBase.button_press_event(self, x, y, num, guiEvent=event)
File "/usr/lib/pymodules/python2.7/matplotlib/backend_bases.py", line 1632, in button_press_event
self.callbacks.process(s, mouseevent)
File "/usr/lib/pymodules/python2.7/matplotlib/cbook.py", line 262, in process
proxy(*args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/cbook.py", line 192, in __call__
return mtd(*args, **kwargs)
File "/home/almarahat/Dropbox/python/GUI/Testing site/test_rt/baud_test.py", line 8, in draw_circle
plt.cla()
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 2784, in cla
ret = gca().cla()
File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 842, in cla
spine.cla()
File "/usr/lib/pymodules/python2.7/matplotlib/spines.py", line 157, in cla
self.axis.cla()
File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 731, in cla
self.reset_ticks()
File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 744, in reset_ticks
self.majorTicks.extend([self._get_tick(major=True)])
File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 1553, in _get_tick
return XTick(self.axes, 0, '', major=major, **tick_kw)
File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 141, in __init__
self.tick2line = self._get_tick2line()
File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 401, in _get_tick2line
l.set_transform(self.axes.get_xaxis_transform(which='tick2'))
File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 586, in get_xaxis_transform
return self.spines['top'].get_spine_transform()
File "/usr/lib/pymodules/python2.7/matplotlib/spines.py", line 374, in get_spine_transform
self._ensure_position_is_set()
File "/usr/lib/pymodules/python2.7/matplotlib/spines.py", line 140, in _ensure_position_is_set
self.set_position(self._position)
File "/usr/lib/pymodules/python2.7/matplotlib/spines.py", line 365, in set_position
self.axis.cla()
File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 731, in cla
self.reset_ticks()
File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 744, in reset_ticks
self.majorTicks.extend([self._get_tick(major=True)])
File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 1553, in _get_tick
return XTick(self.axes, 0, '', major=major, **tick_kw)
File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 140, in __init__
self.tick1line = self._get_tick1line()
File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 383, in _get_tick1line
zorder=self._zorder,
File "/usr/lib/pymodules/python2.7/matplotlib/lines.py", line 195, in __init__
self._marker = MarkerStyle()
File "/usr/lib/pymodules/python2.7/matplotlib/markers.py", line 112, in __init__
self.set_marker(marker)
File "/usr/lib/pymodules/python2.7/matplotlib/markers.py", line 171, in set_marker
self._recache()
File "/usr/lib/pymodules/python2.7/matplotlib/markers.py", line 116, in _recache
self._path = Path(np.empty((0,2)))
File "/usr/lib/pymodules/python2.7/matplotlib/path.py", line 112, in __init__
if ma.isMaskedArray(vertices):
File "/usr/local/lib/python2.7/dist-packages/numpy-1.6.2-py2.7-linux-x86_64.egg/numpy/ma/core.py", line 5683, in isMaskedArray
return isinstance(x, MaskedArray)
RuntimeError: maximum recursion depth exceeded while calling a Python object
At this point, I am not certain where the recursion occurs and how to alleviate this error.
I understand (from other Q&As) that I could enhance my stack limit and get around the problem. However, I don't consider that as a solution in this particular case and would like to get to the bottom of this.
Any help would be appreciate.
Thanks in advance.
Additional Information:
The main functionality that matters include clearing the figure and drawing something new on clicking the canvas.
The clicking does not raise this error, if I am not trying to plot something. Thus, I suspect, I'm missing something in handling matplotlib.
Instead of plt.show(), from within your callback call plt.draw(). The problem is that plt.show runs a mainloop of the GUI library; you just want to update what is shown within the existing mainloop. Using the Qt backend, your code would show the error QCoreApplication::exec: The event loop is already running.
See What is interactive mode? for more guidance on how to use Matplotlib interactively.

Categories