UnboundLocalError: local variable 'x' specified before assignment. Proper use of tsplot in a package for a marine vessel for data?

I cannot get this to work for my data, so first I try to use a concrete example that is very similar. Here is the data frame:

In [56]: idx = pd.DatetimeIndex(start='1990-01-01', freq='d', periods=5) data= pd.DataFrame({('A','a'):[1,2,3,4,5], ('A','b'):[6,7,8,9,1], ('B','a'):[2,3,4,5,6], ('B','b'):[7,8,9,1,2]}, idx) Out[56]: AB abab 1990-01-01 1 6 2 7 1990-01-02 2 7 3 8 1990-01-03 3 8 4 9 1990-01-04 4 9 5 1 1990-01-05 5 1 6 2 

So I hope that this is a time series graph with a line for the central trend among the variables (each column) for each observation (every day in the index) with a shaded area indicating the indicated error rating (probably only 95% qi) of the observations corresponding to each day.

I tried this:

 sns.tsplot(data, time=idx) 

But I get the following error:

 UnboundLocalError Traceback (most recent call last) <ipython-input-57-fa07e08ead95> in <module>() 5 ('B','b'):[7,8,9,1,2]}, idx) 6 ----> 7 sns.tsplot(data, time=idx) C:\Users\Patrick\Anaconda\lib\site-packages\seaborn\timeseries.pyc in tsplot(data, time, unit, condition, value, err_style, ci, interpolate, color, estimator, n_boot, err_palette, err_kws, legend, ax, **kwargs) 253 254 # Pad the sides of the plot only when not interpolating --> 255 ax.set_xlim(x.min(), x.max()) 256 x_diff = x[1] - x[0] 257 if not interpolate: UnboundLocalError: local variable 'x' referenced before assignment 

The syntax for tsplot is:

 sns.tsplot(data, time=None, unit=None, condition=None, value=None, err_style='ci_band', ci=68, interpolate=True, color=None, estimator=<function mean at 0x00000000044F2C18>, n_boot=5000, err_palette=None, err_kws=None, legend=True, ax=None, **kwargs) 

So, I provide my data to the index as an argument to the time, but I'm not sure what I'm doing wrong. I don’t think I need other keyword arguments, but maybe this is a problem.

If I do this with an array with dimensions (unit, time) instead:

 sns.tsplot(data.values.T, time=idx) 

I get the expected result (except without a timestamp - these are xlabels):

enter image description here

But what is the right way to do this with a dataframe? I know that this should be in “long form”, but I'm not quite sure what this will mean for this particular frame.

+6
source share
1 answer

I ended up understanding. Basically the first place I should have looked was here in the section titled “Specifying Input with Long Data Forms”. I had to do this:

 data.reset_index(inplace=True) data.columns = np.arange(len(data.columns)) melted = pd.melt(data, id_vars=0) 

The first row moves the DatetimeIndex to its own column and sets the index of the integer default value inplace. The second line does the same for the headers except throwing them out (I needed to do this because it was not possible to make grouping with multiindex). Finally, we melt the data creating the DataFrame, which looks like this:

 In [120]: melted Out[120]: 0 variable value 0 1990-01-01 1 1 1 1990-01-02 1 2 2 1990-01-03 1 3 3 1990-01-04 1 4 4 1990-01-05 1 5 5 1990-01-01 2 6 6 1990-01-02 2 7 7 1990-01-03 2 8 8 1990-01-04 2 9 9 1990-01-05 2 1 10 1990-01-01 3 2 11 1990-01-02 3 3 12 1990-01-03 3 4 13 1990-01-04 3 5 14 1990-01-05 3 6 15 1990-01-01 4 7 16 1990-01-02 4 8 17 1990-01-03 4 9 18 1990-01-04 4 1 19 1990-01-05 4 2 

Now that the DataFrame is ready, I can use tsplot like this:

 sns.tsplot(melted, time=0, unit='variable', value='value') 

Which in my case is almost the same as if I did:

 sns.tsplot(data.T.values, idx) plt.xlabel('0') plt.ylabel('value') 

in addition, if I added any conditions, then tsplot would build another series and create a legend for me.

It would be nice if tsplot could at least construct dates as timestamps, given the nature of the function. I think using a transposed array will be much easier for my application, and not directly using a DataFrame.

+8
source

Source: https://habr.com/ru/post/979155/


All Articles