imsciences 0.6.1.5__tar.gz → 0.6.1.7__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: imsciences
3
- Version: 0.6.1.5
3
+ Version: 0.6.1.7
4
4
  Summary: IMS Data Processing Package
5
5
  Author: IMS
6
6
  Author-email: cam@im-sciences.com
@@ -209,8 +209,8 @@ class dataprocessing:
209
209
 
210
210
  print("\n37. Replace substrings in column of strings")
211
211
  print(" - Description: Replace substrings in column of strings based off dictionary, can also change column to lower")
212
- print(" - Usage: replace_substrings(df, column, replacements, to_lower=False)")
213
- print(" - Example: replace_substrings(df, 'Influencer Handle', replacement_dict, to_lower=True)")
212
+ print(" - Usage: replace_substrings(df, column, replacements, to_lower=False, new_column=None)")
213
+ print(" - Example: replace_substrings(df, 'Influencer Handle', replacement_dict, to_lower=True, new_column='Short Version')")
214
214
 
215
215
  print("\n38. Add totals column")
216
216
  print(" - Description: Sums all columns with the option to exclude an date column to create a total column")
@@ -1371,28 +1371,38 @@ class dataprocessing:
1371
1371
 
1372
1372
  return df
1373
1373
 
1374
- def replace_substrings(self, df, column, replacements, to_lower=False):
1374
+ def replace_substrings(self, df, column, replacements, to_lower=False, new_column=None):
1375
1375
  """
1376
1376
  Replaces substrings in a column of a DataFrame based on a dictionary of replacements.
1377
- Optionally converts the column values to lowercase.
1377
+ Optionally converts the column values to lowercase and allows creating a new column or modifying the existing one.
1378
1378
 
1379
1379
  Args:
1380
1380
  df (pd.DataFrame): The DataFrame containing the column to modify.
1381
1381
  column (str): The column name where the replacements will be made.
1382
1382
  replacements (dict): A dictionary where keys are substrings to replace and values are the replacement strings.
1383
1383
  to_lower (bool, optional): If True, the column values will be converted to lowercase before applying replacements. Default is False.
1384
+ new_column (str, optional): If provided, the replacements will be applied to this new column. If None, the existing column will be modified. Default is None.
1384
1385
 
1385
1386
  Returns:
1386
1387
  pd.DataFrame: The DataFrame with the specified replacements made, and optionally with lowercase strings.
1387
1388
  """
1388
1389
 
1390
+ if new_column is not None:
1391
+ # Create a new column for replacements
1392
+ df[new_column] = df[column]
1393
+ temp_column = new_column
1394
+ else:
1395
+ # Modify the existing column
1396
+ temp_column = column
1397
+
1389
1398
  # Apply substring replacements
1390
1399
  for old, new in replacements.items():
1391
- df[column] = df[column].str.replace(old, new, regex=False)
1392
-
1400
+ df[temp_column] = df[temp_column].str.replace(old, new, regex=False)
1401
+
1393
1402
  # Optionally convert to lowercase
1394
1403
  if to_lower:
1395
- df[column] = df[column].str.lower()
1404
+ df[temp_column] = df[temp_column].str.lower()
1405
+
1396
1406
  return df
1397
1407
 
1398
1408
  def add_total_column(self, df, exclude_col=None, total_col_name='Total'):
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: imsciences
3
- Version: 0.6.1.5
3
+ Version: 0.6.1.7
4
4
  Summary: IMS Data Processing Package
5
5
  Author: IMS
6
6
  Author-email: cam@im-sciences.com
@@ -8,7 +8,7 @@ def read_md(file_name):
8
8
  return f.read()
9
9
  return ''
10
10
 
11
- VERSION = '0.6.1.5'
11
+ VERSION = '0.6.1.7'
12
12
  DESCRIPTION = 'IMS Data Processing Package'
13
13
  LONG_DESCRIPTION = read_md('README.md') # Reading from README.md
14
14
 
File without changes
File without changes