Skip to main content
deleted 6 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Should function return a reference to Dropping a changed objectrow in terms of readability / clarity, Python 3?a dataframe

I have made a function to drop a row in a pandas.DataFrame. Since, in general, the input dataframe of a function is just a reference to the dataframe. So any change made in that function to the dataframe will be preserved outside that function. Therefore, there are two choices when designing the function:

  1. simply make the change without returning the dataframe.
  2. or, make the change and return the dataframe.

See my code below (function change_df_1 and change_df_2). Could you please let me know which one is better or do you prefer? My concern is readability/clarity of the code. Thanks.

import pandas as pd


def get_df():
    df = pd.DataFrame({'A': [10, 20, 30], 'B':[100, 200, 300]})
    return df


def change_df_1(df: pd.DataFrame):
    df.drop(index=[1], inplace=True)


def change_df_2(df: pd.DataFrame) -> pd.DataFrame:
    df.drop(index=[1], inplace=True)
    return df


def main():
    df = get_df()
    print(f'Original {df=}')

    change_df_1(df=df)
    print(f'Changed_with 1: {df=}')

    # change_df_2(df=df)
    # print(f'Changed_with 2: {df=}')


if __name__ == '__main__':
    main()
```

Should function return a reference to a changed object in terms of readability / clarity, Python 3?

I have made a function to drop a row in a pandas.DataFrame. Since, in general, the input dataframe of a function is just a reference to the dataframe. So any change made in that function to the dataframe will be preserved outside that function. Therefore, there are two choices when designing the function:

  1. simply make the change without returning the dataframe.
  2. or, make the change and return the dataframe.

See my code below (function change_df_1 and change_df_2). Could you please let me know which one is better or do you prefer? My concern is readability/clarity of the code. Thanks.

import pandas as pd


def get_df():
    df = pd.DataFrame({'A': [10, 20, 30], 'B':[100, 200, 300]})
    return df


def change_df_1(df: pd.DataFrame):
    df.drop(index=[1], inplace=True)


def change_df_2(df: pd.DataFrame) -> pd.DataFrame:
    df.drop(index=[1], inplace=True)
    return df


def main():
    df = get_df()
    print(f'Original {df=}')

    change_df_1(df=df)
    print(f'Changed_with 1: {df=}')

    # change_df_2(df=df)
    # print(f'Changed_with 2: {df=}')


if __name__ == '__main__':
    main()
```

Dropping a row in a dataframe

I have made a function to drop a row in a pandas.DataFrame. Since, in general, the input dataframe of a function is just a reference to the dataframe. So any change made in that function to the dataframe will be preserved outside that function. Therefore, there are two choices when designing the function:

  1. simply make the change without returning the dataframe.
  2. or, make the change and return the dataframe.

See my code below (function change_df_1 and change_df_2). Could you please let me know which one is better or do you prefer? My concern is readability/clarity of the code.

import pandas as pd


def get_df():
    df = pd.DataFrame({'A': [10, 20, 30], 'B':[100, 200, 300]})
    return df


def change_df_1(df: pd.DataFrame):
    df.drop(index=[1], inplace=True)


def change_df_2(df: pd.DataFrame) -> pd.DataFrame:
    df.drop(index=[1], inplace=True)
    return df


def main():
    df = get_df()
    print(f'Original {df=}')

    change_df_1(df=df)
    print(f'Changed_with 1: {df=}')

    # change_df_2(df=df)
    # print(f'Changed_with 2: {df=}')


if __name__ == '__main__':
    main()
```
edited tags
Link
Peilonrayz
  • 43.5k
  • 7
  • 76
  • 155
Source Link
aura
  • 251
  • 2
  • 7

Should function return a reference to a changed object in terms of readability / clarity, Python 3?

I have made a function to drop a row in a pandas.DataFrame. Since, in general, the input dataframe of a function is just a reference to the dataframe. So any change made in that function to the dataframe will be preserved outside that function. Therefore, there are two choices when designing the function:

  1. simply make the change without returning the dataframe.
  2. or, make the change and return the dataframe.

See my code below (function change_df_1 and change_df_2). Could you please let me know which one is better or do you prefer? My concern is readability/clarity of the code. Thanks.

import pandas as pd


def get_df():
    df = pd.DataFrame({'A': [10, 20, 30], 'B':[100, 200, 300]})
    return df


def change_df_1(df: pd.DataFrame):
    df.drop(index=[1], inplace=True)


def change_df_2(df: pd.DataFrame) -> pd.DataFrame:
    df.drop(index=[1], inplace=True)
    return df


def main():
    df = get_df()
    print(f'Original {df=}')

    change_df_1(df=df)
    print(f'Changed_with 1: {df=}')

    # change_df_2(df=df)
    # print(f'Changed_with 2: {df=}')


if __name__ == '__main__':
    main()
```