rgwfuncs 0.0.22__py3-none-any.whl → 0.0.23__py3-none-any.whl

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.
rgwfuncs/df_lib.py CHANGED
@@ -1708,47 +1708,36 @@ def print_n_frequency_linear(df: pd.DataFrame, n: int, columns: list, order_by:
1708
1708
 
1709
1709
  return report
1710
1710
 
1711
- def can_convert_keys_to_int(keys):
1712
- """Helper function to check if all keys can be converted to integers."""
1711
+ def try_parse_numeric(val):
1712
+ """Attempt to parse a value as an integer or float."""
1713
1713
  try:
1714
- [int(key) for key in keys]
1715
- return True
1714
+ return int(val)
1716
1715
  except ValueError:
1717
- return False
1716
+ try:
1717
+ return float(val)
1718
+ except ValueError:
1719
+ return val
1718
1720
 
1719
1721
  def sort_frequency(frequency, order_by):
1720
1722
  keys = frequency.keys()
1721
-
1722
- # Determine if we can sort by integer values of keys
1723
- if can_convert_keys_to_int(keys):
1724
- keys = list(map(int, keys)) # Convert keys to integers, and keep original order
1725
1723
 
1726
- if order_by == "ASC":
1727
- return dict(sorted(frequency.items(), key=lambda item: item[0]))
1728
- elif order_by == "DESC":
1729
- return dict(
1730
- sorted(
1731
- frequency.items(),
1732
- key=lambda item: item[0],
1733
- reverse=True))
1734
- elif order_by == "FREQ_ASC":
1735
- return dict(sorted(frequency.items(), key=lambda item: item[1]))
1736
- elif order_by == "BY_KEYS_ASC":
1737
- if isinstance(keys[0], int):
1738
- return dict(sorted(frequency.items(), key=lambda item: int(item[0])))
1739
- else:
1740
- return dict(sorted(frequency.items()))
1741
- elif order_by == "BY_KEYS_DESC":
1742
- if isinstance(keys[0], int):
1743
- return dict(sorted(frequency.items(), key=lambda item: int(item[0]), reverse=True))
1744
- else:
1745
- return dict(sorted(frequency.items(), reverse=True))
1746
- else: # Default to "FREQ_DESC"
1747
- return dict(
1748
- sorted(
1749
- frequency.items(),
1750
- key=lambda item: item[1],
1751
- reverse=True))
1724
+ # Convert keys to numerical values where possible, leaving `NaN` as a special string
1725
+ parsed_keys = [(try_parse_numeric(key), key) for key in keys]
1726
+
1727
+ if order_by in {"BY_KEYS_ASC", "BY_KEYS_DESC"}:
1728
+ reverse = order_by == "BY_KEYS_DESC"
1729
+ sorted_items = sorted(frequency.items(), key=lambda item: try_parse_numeric(item[0]), reverse=reverse)
1730
+ else:
1731
+ if order_by == "ASC":
1732
+ sorted_items = sorted(frequency.items(), key=lambda item: item[0])
1733
+ elif order_by == "DESC":
1734
+ sorted_items = sorted(frequency.items(), key=lambda item: item[0], reverse=True)
1735
+ elif order_by == "FREQ_ASC":
1736
+ sorted_items = sorted(frequency.items(), key=lambda item: item[1])
1737
+ else: # Default to "FREQ_DESC"
1738
+ sorted_items = sorted(frequency.items(), key=lambda item: item[1], reverse=True)
1739
+
1740
+ return dict(sorted_items)
1752
1741
 
1753
1742
  report = generate_linear_report(df, columns, n, order_by)
1754
1743
  print(json.dumps(report, indent=2))
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: rgwfuncs
3
- Version: 0.0.22
3
+ Version: 0.0.23
4
4
  Summary: A functional programming paradigm for mathematical modelling and data science
5
5
  Home-page: https://github.com/ryangerardwilson/rgwfunc
6
6
  Author: Ryan Gerard Wilson
@@ -0,0 +1,9 @@
1
+ rgwfuncs/__init__.py,sha256=2nrp3c5VmVrKh0Ih6zELL8niH9nAHN0XnObqe-EpxlE,1169
2
+ rgwfuncs/df_lib.py,sha256=8KMn4FucI19EFBHUoGOS7R4mo0degg6A6802sjy7BH4,67677
3
+ rgwfuncs/str_lib.py,sha256=I5B0WOGaLUGaedMG7hqiKnIqV7Jc9h1RYlgOiC_-iGY,3678
4
+ rgwfuncs-0.0.23.dist-info/LICENSE,sha256=7EI8xVBu6h_7_JlVw-yPhhOZlpY9hP8wal7kHtqKT_E,1074
5
+ rgwfuncs-0.0.23.dist-info/METADATA,sha256=_mVsZMv4umMXMW_Q2hBxABMm75pKuvJgMIMBldXxCtk,34680
6
+ rgwfuncs-0.0.23.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
7
+ rgwfuncs-0.0.23.dist-info/entry_points.txt,sha256=j-c5IOPIQ0252EaOV6j6STio56sbXl2C4ym_fQ0lXx0,43
8
+ rgwfuncs-0.0.23.dist-info/top_level.txt,sha256=aGuVIzWsKiV1f2gCb6mynx0zx5ma0B1EwPGFKVEMTi4,9
9
+ rgwfuncs-0.0.23.dist-info/RECORD,,
@@ -1,9 +0,0 @@
1
- rgwfuncs/__init__.py,sha256=2nrp3c5VmVrKh0Ih6zELL8niH9nAHN0XnObqe-EpxlE,1169
2
- rgwfuncs/df_lib.py,sha256=nd-F7NGraUINuZUIqYHo2QwUNTjESgBlxKDQbbqlhS8,68050
3
- rgwfuncs/str_lib.py,sha256=I5B0WOGaLUGaedMG7hqiKnIqV7Jc9h1RYlgOiC_-iGY,3678
4
- rgwfuncs-0.0.22.dist-info/LICENSE,sha256=7EI8xVBu6h_7_JlVw-yPhhOZlpY9hP8wal7kHtqKT_E,1074
5
- rgwfuncs-0.0.22.dist-info/METADATA,sha256=aQ86F5Sb5XUGej267Y-hwVuAYqObbuNfy5vcbCxmgYY,34680
6
- rgwfuncs-0.0.22.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
7
- rgwfuncs-0.0.22.dist-info/entry_points.txt,sha256=j-c5IOPIQ0252EaOV6j6STio56sbXl2C4ym_fQ0lXx0,43
8
- rgwfuncs-0.0.22.dist-info/top_level.txt,sha256=aGuVIzWsKiV1f2gCb6mynx0zx5ma0B1EwPGFKVEMTi4,9
9
- rgwfuncs-0.0.22.dist-info/RECORD,,