def __init__(
        self,
        config,
        df,
        labels=['car2go', 'enjoy'],
        agg_func='count',
        save=False,
        norm_per_day=False,
        name='ReB_Hour.pdf',
    ):

        fig, ax = plt.subplots(1, 1, figsize=config['figsize'])

        for label in labels:
            sub_df = df[df.vendor == label]
            sub_df_filter = Filter(sub_df, config)
            sub_df_split_dict = sub_df_filter.split_WD_WE()
            sub_df_we = sub_df_split_dict['WE']
            sub_df_wd = sub_df_split_dict['WD']

            if agg_func == 'count':
                sub_df_we = sub_df_we.groupby('Hour').count()
                sub_df_wd = sub_df_wd.groupby('Hour').count()
            elif agg_func == 'mean':
                sub_df_we = sub_df_we.groupby('Hour').mean()
                sub_df_wd = sub_df_wd.groupby('Hour').mean()
            else:
                print('error')
                return

            sub_df_we = sub_df_we.div(
                self.compute_number_of_days(sub_df_we, norm_per_day))
            sub_df_wd = sub_df_wd.div(
                self.compute_number_of_days(sub_df_we, norm_per_day))

            columns_ref = sub_df_we.columns[0]
            sub_df_we = sub_df_we[columns_ref]
            sub_df_wd = sub_df_wd[columns_ref]
            ax.plot(sub_df_wd.index,
                    sub_df_wd.values,
                    color=config['colors'][label],
                    label='%s Working Days' % label)
            ax.plot(sub_df_we.index,
                    sub_df_we.values,
                    color=config['colors'][label],
                    label='%s Week Ends' % label,
                    linestyle='--')

        ax.tick_params(axis='x', rotation=15)
        ax.legend()
        ax.set_xticks(range(0, 24))
        ax.grid()
        # ax.set_ylim(0,6000)
        ax.set_ylabel('Rentals per hour')
        ax.set_xlabel('Hour of day')

        if save:
            plt.savefig(config['output_plot_path'] + name, bboxinches='tight')
        fig.show()
    def __init__(
        self,
        config,
        df,
        labels=['car2go', 'enjoy'],
        agg_func='count',
        save=False,
        norm_per_day=False,
        name='ReB_Hour.pdf',
    ):

        if norm_per_day == False: div = 1
        else: div = norm_per_day = len(df.Date.unique())

        fig, ax = plt.subplots(1, 1, figsize=config['figsize'])

        for label in labels:
            sub_df = df[df.vendor == label]
            sub_df_filter = Filter(sub_df, config)
            sub_df_split_dict = sub_df_filter.split_WD_WE()
            sub_df_we = sub_df_split_dict['WE']
            sub_df_wd = sub_df_split_dict['WD']
            column_ref = df.columns[0]
            # c2g_number_of_workingdays = c2g_wd.groupby('Wod').count()[column_ref].sum()
            # c2g_number_of_weekends = c2g_we.groupby('Wod').count()[column_ref].sum()

            sub_df_we = sub_df_we.groupby('Hour')[column_ref].agg(
                agg_func).div(div)
            sub_df_wd = sub_df_wd.groupby('Hour')[column_ref].agg(
                agg_func).div(div)

            ax.plot(sub_df_wd.index,
                    sub_df_wd.values,
                    color=config['colors_per_city'][label],
                    label='%s Working Days' % label)
            ax.plot(sub_df_we.index,
                    sub_df_we.values,
                    color=config['colors_per_city'][label],
                    label='%s Week Ends' % label,
                    linestyle='--')

        ax.tick_params(axis='x', rotation=15)
        ax.legend()
        ax.set_xticks(range(0, 24))
        ax.grid()
        # ax.set_ylim(0,6000)
        ax.set_ylabel('Rentals per day')
        ax.set_xlabel('Hour of day')

        if save:
            plt.savefig(config['output_plot_path'] + 'ReB_Hour.pdf',
                        bboxinches='tight')
        fig.show()
Example #3
0
    def __init__(self, df, city_map, config, label, save=False):

        df_before_filters = df.copy()

        filter = Filter(df, config)
        filter.remove_fake_bookings_torino()
        filter.date_standardization()
        df = filter.rentals()

        converter = DF2GDF()
        df_start = converter.df2gdf_point(df.copy(),
                                          dict(Lon='start_lon',
                                               Lat='start_lat'),
                                          crs=config['crs'])
        df_end = converter.df2gdf_point(df.copy(),
                                        dict(Lon='end_lon', Lat='end_lat'),
                                        crs=config['crs'])

        df = converter.assign_zone_to_rental(df_start,
                                             df_end,
                                             city_map,
                                             convert_dict_start={
                                                 'index_right':
                                                 'start_zone_ID',
                                                 'zone_name': 'start_zone_name'
                                             },
                                             convert_dict_end={
                                                 'index_right': 'end_zone_ID',
                                                 'zone_name': 'end_zone_name'
                                             })
        df = df[df.start_zone_ID.notna()]
        df = df[df.end_zone_ID.notna()]

        converter.dump_df(df, label + 'Torino', config['data_path'])

        city_map_cleaned = converter.drop_zones_without_bookings(df, city_map)
        '''Morning bookings'''
        df_m = df[df.Hour.isin(list(range(7, 13)))]
        df_m = df_m.groupby('end_zone_ID').count()['_id']
        '''Evening'''
        df_e = df[df.Hour.isin(list(range(17, 22)))]
        df_e = df_e.groupby('end_zone_ID').count()['_id']

        city_map_cleaned['actract'] = df_e - df_m
        city_map_cleaned = converter.reshape_caselle(city_map_cleaned,
                                                     config['caselleID'])
        city_map_cleaned = city_map_cleaned.dropna()

        fig, ax = plt.subplots()
        city_map_cleaned.plot(ax=ax, column='actract', legend=True)
        ax.set_xticks([])
        ax.set_yticks([])

        if save: plt.savefig(config['output_plot_path'] + label + 'Torino.pdf')

        fig.show()
Example #4
0
    def __init__(self, config, df, labels=['car2go', 'enjoy']):
        self.df = df
        self.config = config

        self.datasets = {}

        for label in labels:
            sub_df = self.df[df.vendor == label]
            self.datasets[label] = {}
            self.datasets[label]['bookings'] = {}
            self.datasets[label]['rentals '] = {}

            filter = Filter(sub_df, config)
            out_b = filter.split_WD_WE()
            self.datasets[label]['bookings'] = out_b
            filter.rentals()
            out_r = filter.split_WD_WE()
            self.datasets[label]['rentals'] = out_r
        self.labels = labels
Example #5
0
    nrows = 120000
    config = ReadConfig('config.json')
    plt.rcParams.update({'font.size': config['fs']})

    col2keep = [
        '_id', 'city', 'distance', 'driving_distance', 'driving_duration',
        'duration', 'end_lat', 'end_lon', 'final_address', 'final_date',
        'final_fuel', 'final_time', 'init_address', 'init_date', 'init_fuel',
        'init_time', 'plate', 'pt_arrival_date', 'pt_arrival_time',
        'pt_distance', 'pt_duration', 'start_lat', 'start_lon', 'vendor',
        'walking_distance', 'walking_duration'
    ]

    c2g = Loader(config, 'Torino', 'car2go', nrows)
    c2g = c2g[col2keep]
    c2g_filter = Filter(c2g, config)
    c2g_filter.date_standardization(fmt='%Y-%m-%d %H:%M:%S')
    c2g_filter.remove_fake_bookings_torino()
    c2g_allevents = c2g.copy()
    c2g_filter.rentals()
    c2g = c2g_filter.df

    enj = Loader(config, 'Torino', 'enjoy', nrows)
    enj = enj[col2keep]
    enj_filter = Filter(enj, config)
    enj_filter.date_standardization(fmt='%Y-%m-%d %H:%M:%S')
    enj_filter.remove_fake_bookings_torino()
    enj_allevents = enj.copy()
    enj_filter.rentals()
    enj = enj_filter.df
Example #6
0
    minn = minn.rename(columns=new_columns)

    not_formatted_init = minn[minn.init_date.str.contains('/')].index
    minn.loc[not_formatted_init, 'init_date'] = minn.init_date.str.replace(
        '/', '-').str.replace(' ', 'T')
    minn.loc[not_formatted_init,
             'init_date'] = minn.loc[not_formatted_init, 'init_date'] + '.000Z'

    not_formatted_final = minn[minn.final_date.str.contains('/')].index
    minn.loc[not_formatted_init, 'final_date'] = minn.init_date.str.replace(
        '/', '-').str.replace(' ', 'T')
    minn.loc[not_formatted_init,
             'final_date'] = minn.loc[not_formatted_init,
                                      'final_date'] + '.000Z'

    minn_filter = Filter(minn, config)
    minn_filter.date_standardization(fmt='%Y-%m-%dT%H:%M:%S.000Z')
    minn_filter.localize_timezone('Date_index', 'UTC', 'America/Chicago')
    minn_filter.limit_date()

    minn_allevents = minn.copy()
    minn = minn_filter.df
    minn['Date'] = minn.Date_index.dt.date
    minn_allevents['Date'] = minn_allevents.Date_index.dt.date

    louis = Loader(config, l2, 'car2go', nrows)
    louis = louis[~louis.StartTime.str.contains('24:')]
    louis['StartTime'] = louis['StartDate'] + 'T' + louis[
        'StartTime'] + ':00.000Z'
    louis['vendor'] = 'Louisville'
    louis['TripDuration'] = louis['TripDuration'].mul(60)