wong david
wong david

Reputation: 43

Sorting: 'DataFrame' object has no attribute 'sort'

The code is written in python 3.4 but I want to run it on python 3.7. I am using the Jupyter notebook to run. Can I create an environment in Jupyter and run the code in an older version of pandas(e.g. 0.16)?

--> 210             tmpvals[n] = find_non_overlapping_sample(
    211                 df.sort_index(key, ascending=phase).head(600).index, prd = no_overlap_prd)        
    212 

~\Anaconda3\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
   3079             if name in self._info_axis:
   3080                 return self[name]
-> 3081             return object.__getattribute__(self, name)
   3082 
   3083     def __setattr__(self, name, value):

AttributeError: 'DataFrame' object has no attribute 'sort' `

Even I change the sort to sort_index or sort_values. The error still exists.

Here's the function(the original code).

def gen_keydates(df, key, seasonalData=False, no_overlap_prd = 365, chatty=True):

    tmpvals={}
    if seasonalData: # For DJF data use modified function
        for n, phase in enumerate([False, True]): # Maximum, Minimum Phase
            tmpvals[n] = findNonOverlappingSeasonSample(
                df.sort(key, ascending=phase).head(600).index, key=key)
    else:
        for n, phase in enumerate([False, True]): # Maximum, Minimum Phase
            tmpvals[n] = find_non_overlapping_sample(
                df.sort(key, ascending=phase).head(600).index, prd = no_overlap_prd)        
    compPhase ={}
    compPhase['max'] = tmpvals[0][0:11].order()
    compPhase['min'] = tmpvals[1][0:11].order()
    if chatty:
        print("Average for max/min sample of {0}: {1:2.3f} and {2:2.3f}".format(
            key,np.mean(df[key][compPhase['max']]),
            np.mean(df[key][compPhase['min']])))
    return compPhase`

Upvotes: 1

Views: 3986

Answers (1)

Alex Ozerov
Alex Ozerov

Reputation: 1028

Sort method has been deprecated.
You should go for df.sort_values() or df.sort_index() methods.

Upvotes: 2

Related Questions