<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _update_pypsa_timeseries_by_type(network, type, components_to_update=None, timesteps=None):
 """ Updates time series of specified component in pypsa representation. Be aware that if you call this function with `timesteps` and thus overwrite current time steps it may lead to inconsistencies in the pypsa network since only time series of the specified component are updated but none of the other time series or the snapshots attribute of the pypsa network. Use the function :func:`update_pypsa_timeseries` to change the time steps you want to analyse in the power flow analysis. This function will raise an error when a component that is currently not in the pypsa representation is added. Parameters network : Network The eDisGo grid topology model overall container type : :obj:`str` Type specifies the type of component (load, generator or storage) that is updated. components_to_update : :obj:`list`, optional List with all components (either of type :class:`~.grid.components.Load`, :class:`~.grid.components.Generator` or :class:`~.grid.components.Storage`) that need to be updated. Possible options are 'load', 'generator' and 'storage'. Components in list must all be of the same type. If None all components specified by `type` are updated depending on the mode. See :meth:`~.tools.pypsa_io.to_pypsa` for more information on mode. timesteps : :pandas:`pandas.DatetimeIndex<datetimeindex>` or :pandas:`pandas.Timestamp<timestamp>` Timesteps specifies which time steps of the time series to export to pypsa representation. If None all time steps currently existing in pypsa representation are updated. If not None current time steps are overwritten by given time steps. Default: None. """


    # pypsa dataframe to update
    if type == 'load':
        pypsa_ts = network.pypsa.loads_t
        components_in_pypsa = network.pypsa.loads.index
    elif type == 'generator':
        pypsa_ts = network.pypsa.generators_t
        components_in_pypsa = network.pypsa.generators.index
    elif type == 'storage':
        pypsa_ts = network.pypsa.storage_units_t
        components_in_pypsa = network.pypsa.storage_units.index
    else:
        raise ValueError('{} is not a valid type.'.format(type))

    # MV and LV loads
    if network.pypsa.edisgo_mode is None:
        # if no components are specified get all components of specified type
        # in whole grid
        if components_to_update is None:
            grids = [network.mv_grid] + list(network.mv_grid.lv_grids)
            if type == 'generator':
                components_to_update = list(itertools.chain(
                    *[grid.generators for grid in grids]))
            else:
                components_to_update = list(itertools.chain(
                    *[grid.graph.nodes_by_attribute(type) for grid in grids]))
        # if no time steps are specified update all time steps currently
        # contained in pypsa representation
        if timesteps is None:
            timesteps = pypsa_ts.p_set.index
        # check if timesteps is array-like, otherwise convert to list
        # (necessary to avoid getting a scalar using .loc)
        if not hasattr(timesteps, "__len__"):
            timesteps = [timesteps]

        p_set = pd.DataFrame()
        q_set = pd.DataFrame()
        for comp in components_to_update:
            if repr(comp) in components_in_pypsa:
                p_set[repr(comp)] = comp.pypsa_timeseries('p').loc[timesteps]
                q_set[repr(comp)] = comp.pypsa_timeseries('q').loc[timesteps]
            else:
                raise KeyError("Tried to update component {} but could not "
                               "find it in pypsa network.".format(comp))
        # overwrite pypsa time series
        pypsa_ts.p_set = p_set
        pypsa_ts.q_set = q_set

    # MV and aggregated LV loads
    elif network.pypsa.edisgo_mode is 'mv':
        raise NotImplementedError

    # LV only
    elif network.pypsa.edisgo_mode is 'lv':
        raise NotImplementedError