geost.accessor.GeostFrame.merge_sorted#

GeostFrame.merge_sorted(other: DataFrame, keep_surveys: Literal['left', 'right', 'outer', 'inner'] = 'outer', backfill: bool = True, relative_to_reference: bool = False, drop_overlapping_columns: bool = False) → DataFrame[source]#

Insert intervals from another DataFrame into the current DataFrame, sorted by survey and depth. Intervals from other are inserted at their corresponding depths and can split existing intervals at their top and bottom boundaries. Use backfill=True to propagate values from inserted intervals to preceding intervals within each survey, as shown in the examples below.

Parameters:
  • other (pd.DataFrame) – DataFrame containing intervals to insert into the current DataFrame.

  • keep_surveys (Literal["left", "right", "outer", "inner"], optional) – Determines which surveys to keep when merging intervals. Can be “left”, “right”, “outer”, or “inner”. The default is “outer”. - “left”: Keep only surveys present in the current DataFrame. - “right”: Keep only surveys present in the other DataFrame. - “outer”: Keep all surveys from both DataFrames. - “inner”: Keep only surveys present in both DataFrames.

  • backfill (bool, optional) – If True, backfill missing values within each survey from inserted intervals to preceding intervals. The default is True.

  • relative_to_reference (bool, optional) – If True, interpret the depths of the other DataFrame relative to a reference level such as “NAP”. The default is False, in this case depths are interpreted as absolute depth.

  • drop_overlapping_columns (bool, optional) – If True, drop columns from the other DataFrame that overlap with columns in the current DataFrame. The default is False, then overlapping columns are kept with the suffix “_right” in the result.

Returns:

A new DataFrame containing the merged and depth-sorted data.

Return type:

pd.DataFrame

Examples

>>> import geost
>>> import pandas as pd
>>> boreholes = pd.DataFrame(
...     {
...         "nr": ["A", "A", "B", "B", "C", "C"],
...         "surface": [0.2, 0.2, 0.3, 0.3, 0.25, 0.25],
...         "top": [0, 0.8, 0, 1.0, 0, 0.4],
...         "bottom": [0.8, 1.2, 1.0, 1.5, 0.4, 0.8],
...         "lith": ["K", "K", "Z", "K", "K", "L"],
...     }
... )
>>> boreholes
  nr  surface  top  bottom lith
0  A     0.20  0.0     0.8    K
1  A     0.20  0.8     1.2    K
2  B     0.30  0.0     1.0    Z
3  B     0.30  1.0     1.5    K
4  C     0.25  0.0     0.4    K
5  C     0.25  0.4     0.8    L
>>> to_merge = pd.DataFrame(
...        {
...            "nr": ["A", "B", "D"],
...            "surface": [0.3, 0.4, 0.15],
...            "top": [0.0, 0.0, 0.0],
...            "bottom": [1.1, 2.0, 0.4],
...            "value": [15, 35, 45],
...        }
...    )
>>> to_merge
  nr  surface  top  bottom  value
0  A     0.30  0.0     1.1     15
1  B     0.40  0.0     2.0     35
2  D     0.15  0.0     0.4     45

By default, all survey IDs are preserved from the left and right DataFrames (i.e. keep_surveys='outer').

>>> result = boreholes.gst.merge_sorted(to_merge, backfill=True)
>>> result
  nr  surface  top  bottom lith  surface_right  value
0  A     0.20  0.0     0.8    K           0.30   15.0
1  A     0.20  0.8     1.0    K           0.30   15.0
2  A     0.20  1.0     1.2    K            NaN    NaN
3  B     0.30  0.0     1.0    Z           0.40   35.0
4  B     0.30  1.0     1.5    K           0.40   35.0
5  B     0.30  1.5     1.9  NaN           0.40   35.0
6  C     0.25  0.0     0.4    K            NaN    NaN
7  C     0.25  0.4     0.8    L            NaN    NaN
8  D     0.15  0.0     0.4  NaN           0.15   45.0
>>> result = boreholes.gst.merge_sorted(to_merge, backfill=False)
>>> result
  nr  surface  top  bottom lith  surface_right  value
0  A     0.20  0.0     0.8    K            NaN    NaN
1  A     0.20  0.8     1.0  NaN           0.30   15.0
2  A     0.20  1.0     1.2    K            NaN    NaN
3  B     0.30  0.0     1.0    Z            NaN    NaN
4  B     0.30  1.0     1.5    K            NaN    NaN
5  B     0.30  1.5     1.9  NaN           0.40   35.0
6  C     0.25  0.0     0.4    K            NaN    NaN
7  C     0.25  0.4     0.8    L            NaN    NaN
8  D     0.15  0.0     0.4  NaN           0.15   45.0

If you only want to keep surveys that are present in both DataFrames and drop the overlapping columns, use keep_surveys='inner' and drop_overlapping_columns=True.

>>> result = boreholes.gst.merge_sorted(
...     to_merge, keep_surveys="inner", drop_overlapping_columns=True
... )
>>> result
  nr  surface  top  bottom lith  value
0  A      0.2  0.0     0.8    K   15.0
1  A      0.2  0.8     1.0    K   15.0
2  A      0.2  1.0     1.2    K    NaN
3  B      0.3  0.0     1.0    Z   35.0
4  B      0.3  1.0     1.5    K   35.0
5  B      0.3  1.5     1.9  NaN   35.0