Repository: pyjanitor-devs/pyjanitor

Issue:
[ENH] Add a reset_index boolean kwarg to shuffle()
# Brief Description

I would like to propose to add a `reset_index` boolean kwarg to the `.shuffle()` function.

Doing so would help save a line of code. After shuffling a dataframe, the index remains the same. Sometimes, users may want to preserve the index, but my current opinionated opinion is that preservation of the index might not be the 90% use case. Instead, we want to shuffle the rows without keeping each row attached to its original index, i.e. we would want to reset_index.

# Example API

We would just add the reset_index kwarg to shuffle.

```python
df.shuffle(reset_index=True)
```

Underneath the hood, it would simply call .reset_index(drop=True):

```python
def shuffle(df, ..., reset_index=True):
    ...
    ...
    if reset_index:
        df = df.reset_index(drop=True)
    return df
```

[ENH] Add a reset_index boolean kwarg to shuffle()
# Brief Description

I would like to propose to add a `reset_index` boolean kwarg to the `.shuffle()` function.

Doing so would help save a line of code. After shuffling a dataframe, the index remains the same. Sometimes, users may want to preserve the index, but my current opinionated opinion is that preservation of the index might not be the 90% use case. Instead, we want to shuffle the rows without keeping each row attached to its original index, i.e. we would want to reset_index.

# Example API

We would just add the reset_index kwarg to shuffle.

```python
df.shuffle(reset_index=True)
```

Underneath the hood, it would simply call .reset_index(drop=True):

```python
def shuffle(df, ..., reset_index=True):
    ...
    ...
    if reset_index:
        df = df.reset_index(drop=True)
    return df
```



Discussion:
@ericmjl 
I would love to work on this 
@ericmjl 
I would love to work on this 

Resolving patch:
diff --git a/janitor/functions.py b/janitor/functions.py
--- a/janitor/functions.py
+++ b/janitor/functions.py
@@ -3321,7 +3321,9 @@ def take_first(
 
 
 @pf.register_dataframe_method
-def shuffle(df: pd.DataFrame, random_state=None) -> pd.DataFrame:
+def shuffle(
+    df: pd.DataFrame, random_state=None, reset_index=True
+) -> pd.DataFrame:
     """
     Shuffle the rows of the DataFrame.
 
@@ -3339,7 +3341,10 @@ def shuffle(df: pd.DataFrame, random_state=None) -> pd.DataFrame:
     :param df: A pandas DataFrame
     :param random_state: (optional) A seed for the random number generator.
     """
-    return df.sample(frac=1, random_state=random_state)
+    result = df.sample(frac=1, random_state=random_state)
+    if reset_index:
+        result = result.reset_index(drop=True)
+    return result
 
 
 @pf.register_dataframe_method


Test patch:
diff --git a/tests/functions/test_shuffle.py b/tests/functions/test_shuffle.py
--- a/tests/functions/test_shuffle.py
+++ b/tests/functions/test_shuffle.py
@@ -2,13 +2,25 @@
 
 
 @pytest.mark.functions
-def test_shuffle(dataframe):
+def test_shuffle_without_index_reset(dataframe):
     """
     Test the shuffle function.
 
     This test checks that the set of indices in the shuffled dataframe are
     identical to the set of indices in the original.
     """
-    df = dataframe.shuffle()
-
+    df = dataframe.shuffle(reset_index=False)
     assert set(df.index) == set(dataframe.index)
+
+
+@pytest.mark.functions
+def test_shuffle(dataframe):
+    """
+    Test the shuffle function.
+
+    This test checks that the set of dataframes has identical columns and
+    number of rows.
+    """
+    df = dataframe.shuffle()
+    assert len(df) == len(dataframe)
+    assert set(df.columns) == set(dataframe.columns)