def sort_data(path, dt):
    '\n    Check if path has sorted data tag (_sorted)\n    If not check if data has previously been sorted and exists in the directory.\n    Check to see if the value dt is correct\n    If not sort data and write filename_sorted.csv\n\n    data is sorted by time in ascending order and downsampled to a frequency of dt seconds.\n    Missing values are interpolated.\n    A time-date string is also inserted.\n    '

    def sort(path, dt):
        df = pd.read_csv(path)
        if (path[(- 11):] == '_sorted.csv'):
            path_sorted = path
        else:
            path_sorted = (path[:(- 4)] + '_sorted.csv')
        df = df.sort_values(by=['time'], ascending=True)
        try:
            df.insert(loc=0, column='date-time', value=pd.to_datetime(df['time'], unit='ms'))
        except ValueError:
            raise ValueError('Data appears to have already been sorted. Check if still appropriate and add _sorted.csv tag to avoid this error.')
        df = df.set_index('date-time').resample((str(dt) + 's')).mean().round(2)
        df['time'] = ((df.index - pd.Timestamp('1970-01-01')) // pd.Timedelta('1s'))
        df = df.tz_localize('Europe/London')
        df = df.interpolate().round(2)
        df.to_csv(path_sorted, index=True)

    def need_to_sort(path, dt):

        def get_dt(path):
            df_dt = pd.read_csv(path)['time'][0:2].values
            return (df_dt[1] - df_dt[0])
        if (path[(- 11):] == '_sorted.csv'):
            if (get_dt(path) == dt):
                return False
            else:
                return True
        else:
            path_sorted = (path[:(- 4)] + '_sorted.csv')
            import os.path
            if os.path.isfile(path_sorted):
                if (get_dt(path_sorted) == dt):
                    return False
                else:
                    return True
            else:
                return True
    if need_to_sort(path, dt):
        sort(path, dt)
    if (path[(- 11):] == '_sorted.csv'):
        path_sorted = path
    else:
        path_sorted = (path[:(- 4)] + '_sorted.csv')
    return path_sorted
Check if path has sorted data tag (_sorted)
If not check if data has previously been sorted and exists in the directory.
Check to see if the value dt is correct
If not sort data and write filename_sorted.csv

data is sorted by time in ascending order and downsampled to a frequency of dt seconds.
Missing values are interpolated.
A time-date string is also inserted.
def sort_data(path, dt):
    '\n    Check if path has sorted data tag (_sorted)\n    If not check if data has previously been sorted and exists in the directory.\n    Check to see if the value dt is correct\n    If not sort data and write filename_sorted.csv\n\n    data is sorted by time in ascending order and downsampled to a frequency of dt seconds.\n    Missing values are interpolated.\n    A time-date string is also inserted.\n    '

    def sort(path, dt):
        df = pd.read_csv(path)
        if (path[(- 11):] == '_sorted.csv'):
            path_sorted = path
        else:
            path_sorted = (path[:(- 4)] + '_sorted.csv')
        df = df.sort_values(by=['time'], ascending=True)
        try:
            df.insert(loc=0, column='date-time', value=pd.to_datetime(df['time'], unit='ms'))
        except ValueError:
            raise ValueError('Data appears to have already been sorted. Check if still appropriate and add _sorted.csv tag to avoid this error.')
        df = df.set_index('date-time').resample((str(dt) + 's')).mean().round(2)
        df['time'] = ((df.index - pd.Timestamp('1970-01-01')) // pd.Timedelta('1s'))
        df = df.tz_localize('Europe/London')
        df = df.interpolate().round(2)
        df.to_csv(path_sorted, index=True)

    def need_to_sort(path, dt):

        def get_dt(path):
            df_dt = pd.read_csv(path)['time'][0:2].values
            return (df_dt[1] - df_dt[0])
        if (path[(- 11):] == '_sorted.csv'):
            if (get_dt(path) == dt):
                return False
            else:
                return True
        else:
            path_sorted = (path[:(- 4)] + '_sorted.csv')
            import os.path
            if os.path.isfile(path_sorted):
                if (get_dt(path_sorted) == dt):
                    return False
                else:
                    return True
            else:
                return True
    if need_to_sort(path, dt):
        sort(path, dt)
    if (path[(- 11):] == '_sorted.csv'):
        path_sorted = path
    else:
        path_sorted = (path[:(- 4)] + '_sorted.csv')
    return path_sorted