Repository: modin-project/modin

Issue:
New API: `truncate` for Series and DataFrame

### Describe the problem
<!-- Describe the problem clearly here. -->
Now that #1024 is fixed, `truncate` can be implemented easier using the `loc` logic.




Discussion:

Resolving patch:
diff --git a/modin/pandas/base.py b/modin/pandas/base.py
--- a/modin/pandas/base.py
+++ b/modin/pandas/base.py
@@ -3151,9 +3151,15 @@ def truediv(self, other, axis="columns", level=None, fill_value=None):
     div = divide = truediv
 
     def truncate(self, before=None, after=None, axis=None, copy=True):
-        return self._default_to_pandas(
-            "truncate", before=before, after=after, axis=axis, copy=copy
-        )
+        axis = self._get_axis_number(axis) if axis is not None else 0
+        if (
+            not self.axes[axis].is_monotonic_increasing
+            and not self.axes[axis].is_monotonic_decreasing
+        ):
+            raise ValueError("truncate requires a sorted index")
+        s = slice(*self.axes[axis].slice_locs(before, after))
+        slice_obj = s if axis == 0 else (slice(None), s)
+        return self.iloc[slice_obj]
 
     def tshift(self, periods=1, freq=None, axis=0):
         axis = self._get_axis_number(axis)


Test patch:
diff --git a/modin/pandas/test/test_dataframe.py b/modin/pandas/test/test_dataframe.py
--- a/modin/pandas/test/test_dataframe.py
+++ b/modin/pandas/test/test_dataframe.py
@@ -2639,10 +2639,52 @@ def test_to_xarray(self):
         with pytest.warns(UserWarning):
             pd.DataFrame(data).to_xarray()
 
-    def test_truncate(self):
-        data = test_data_values[0]
-        with pytest.warns(UserWarning):
-            pd.DataFrame(data).truncate()
+    @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys)
+    def test_truncate(self, data):
+        modin_df = pd.DataFrame(data)
+        pandas_df = pandas.DataFrame(data)
+
+        before = 1
+        after = len(modin_df - 3)
+        df_equals(modin_df.truncate(before, after), pandas_df.truncate(before, after))
+
+        before = 1
+        after = 3
+        df_equals(modin_df.truncate(before, after), pandas_df.truncate(before, after))
+
+        before = modin_df.columns[1]
+        after = modin_df.columns[-3]
+        try:
+            pandas_result = pandas_df.truncate(before, after, axis=1)
+        except Exception as e:
+            with pytest.raises(type(e)):
+                modin_df.truncate(before, after, axis=1)
+        else:
+            modin_result = modin_df.truncate(before, after, axis=1)
+            df_equals(modin_result, pandas_result)
+
+        before = modin_df.columns[1]
+        after = modin_df.columns[3]
+        try:
+            pandas_result = pandas_df.truncate(before, after, axis=1)
+        except Exception as e:
+            with pytest.raises(type(e)):
+                modin_df.truncate(before, after, axis=1)
+        else:
+            modin_result = modin_df.truncate(before, after, axis=1)
+            df_equals(modin_result, pandas_result)
+
+        before = None
+        after = None
+        df_equals(modin_df.truncate(before, after), pandas_df.truncate(before, after))
+        try:
+            pandas_result = pandas_df.truncate(before, after, axis=1)
+        except Exception as e:
+            with pytest.raises(type(e)):
+                modin_df.truncate(before, after, axis=1)
+        else:
+            modin_result = modin_df.truncate(before, after, axis=1)
+            df_equals(modin_result, pandas_result)
 
     def test_tshift(self):
         idx = pd.date_range("1/1/2012", periods=5, freq="M")
diff --git a/modin/pandas/test/test_series.py b/modin/pandas/test/test_series.py
--- a/modin/pandas/test/test_series.py
+++ b/modin/pandas/test/test_series.py
@@ -2629,9 +2629,25 @@ def test_truediv(data):
 
 @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys)
 def test_truncate(data):
-    modin_series, _ = create_test_series(data)  # noqa: F841
-    with pytest.warns(UserWarning):
-        modin_series.truncate()
+    modin_series, pandas_series = create_test_series(data)
+
+    before = 1
+    after = len(modin_series - 3)
+    df_equals(
+        modin_series.truncate(before, after), pandas_series.truncate(before, after)
+    )
+
+    before = 1
+    after = 3
+    df_equals(
+        modin_series.truncate(before, after), pandas_series.truncate(before, after)
+    )
+
+    before = None
+    after = None
+    df_equals(
+        modin_series.truncate(before, after), pandas_series.truncate(before, after)
+    )
 
 
 def test_tshift():