I am trying to merge 4 rasters together using the rasterio.merge function. All four rasters have the same CRS and resolution, but they all have different, but overlapping extents. The extent of all four rasters overlap each other, but there are no pixels with positive values that overlap from the different rasters. The merge was partially successful as a new raster was formed; however, the merged raster only shows nodata values for pixels that exist in the area where the extents overlap.
Four input rasters displayed together (north, east, west, and south EU)

I have tried to use the rasterio.merge (as merge) function:
mosaic, output = merge(raster_to_mosaic, method = 'first', nodata = -9999)
and I tried all merge methods (first, last, min, max, count, sum)
I expected to get a raster back which overrided pixels nodata that were overlapping with pixels with real values.
Whole script:
import rasterio as rio
from rasterio.merge import merge
from pathlib import Path
east_rent_filepath = "/merged/east_SSP1_2020_rent_hectare_filtered.tif"
north_rent_filepath = "/merged/north_SSP1_2020_rent_hectare_filtered.tif"
west_rent_filepath = "/merged/west_SSP1_2020_rent_hectare_filtered.tif"
south_rent_filepath = "/merged/south_SSP1_2020_rent_hectare_filtered.tif"
out_path = "/merged/merged_count.tif"
# Open the raster file for reading and writing
with rasterio.open(east_rent_filepath, "r+") as src:
# Read the data into a numpy array
data = src.read(1)
# Set the NoData value
data[data == -9999] = src.nodata
# Update the raster with the new data
src.write(data, 1)
# Open the raster file for reading and writing
with rasterio.open(north_rent_filepath, "r+") as src:
# Read the data into a numpy array
data = src.read(1)
# Set the NoData value
data[data == -9999] = src.nodata
# Update the raster with the new data
src.write(data, 1)
# Open the raster file for reading and writing
with rasterio.open(west_rent_filepath, "r+") as src:
# Read the data into a numpy array
data = src.read(1)
# Set the NoData value
data[data == -9999] = src.nodata
# Update the raster with the new data
src.write(data, 1)
# Open the raster file for reading and writing
with rasterio.open(south_rent_filepath, "r+") as src:
# Read the data into a numpy array
data = src.read(1)
# Set the NoData value
data[data == -9999] = src.nodata
# Update the raster with the new data
src.write(data, 1)
print("updated nodata to -9999")
path = Path('/merged/')
Path('output').mkdir(parents=True, exist_ok=True)
raster_files = list(path.iterdir())
raster_to_mosaic = []
for p in raster_files:
raster = rio.open(p)
raster_to_mosaic.append(raster)
mosaic, output = merge(raster_to_mosaic, method = 'first', nodata = -9999)
output_meta = raster.meta.copy()
output_meta.update(
{"driver": "GTiff",
"height": mosaic.shape[1],
"width": mosaic.shape[2],
"transform": output,
}
)
with rio.open(out_path, "w", **output_meta) as m:
m.write(mosaic)

Resolved this issue by a work around. I first clipped all rasters to a common extent in QGIS using the "Clip raster by extent" function (used a raster layer extent which included all the rasters) and then I used the following code to stack the rasters on top of each other and ignoring any NaN values, and found the maximum value across the four rasters: