microdf-python 0.4.4__py3-none-any.whl → 0.4.6__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.
microdf/utils.py CHANGED
@@ -1,40 +1,40 @@
1
1
  import collections
2
+ from typing import Dict, List, Optional, Union
2
3
 
3
4
  import pandas as pd
4
5
 
5
6
 
6
- def ordinal_label(n):
7
+ def ordinal_label(n: int) -> str:
7
8
  """Creates ordinal label from number.
8
9
 
9
10
  Adapted from https://stackoverflow.com/a/20007730/1840471.
10
11
 
11
12
  :param n: Number.
12
13
  :returns: Ordinal label, e.g., 1st, 3rd, 24th, etc.
13
-
14
14
  """
15
15
  n = int(n)
16
16
  ix = (n / 10 % 10 != 1) * (n % 10 < 4) * n % 10
17
17
  return "%d%s" % (n, "tsnrhtdd"[ix::4])
18
18
 
19
19
 
20
- def dedup_list(lst):
20
+ def dedup_list(lst: List) -> List:
21
21
  """Remove duplicate items from a list.
22
22
 
23
23
  :param lst: List.
24
24
  :returns: List with duplicate items removed from lst.
25
-
26
25
  """
27
26
  return list(set(lst))
28
27
 
29
28
 
30
- def listify(x, dedup=True):
29
+ def listify(
30
+ x: Union[str, List[str]], dedup: Optional[bool] = True
31
+ ) -> List[str]:
31
32
  """Return x as a list, if it isn't one already.
32
33
 
33
34
  :param x: A single item or a list
34
35
  :param dedup: Default value = True)
35
- :returns: x if x is a list, otherwise [x]. Also flattens the list
36
- and removes Nones.
37
-
36
+ :returns: x if x is a list, otherwise [x]. Also flattens the list and
37
+ removes Nones.
38
38
  """
39
39
  if not isinstance(x, list):
40
40
  x = [x]
@@ -45,7 +45,7 @@ def listify(x, dedup=True):
45
45
  return res
46
46
 
47
47
 
48
- def flatten(lst):
48
+ def flatten(lst: List) -> None:
49
49
  """Flatten list. From https://stackoverflow.com/a/2158532/1840471.
50
50
 
51
51
  :param lst: List.
@@ -61,14 +61,13 @@ def flatten(lst):
61
61
  yield el
62
62
 
63
63
 
64
- def cartesian_product(d):
65
- """Produces a DataFrame as a Cartesian product of dictionary
66
- keys and values.
64
+ def cartesian_product(d: Dict) -> pd.DataFrame:
65
+ """Produces a DataFrame as a Cartesian product of dictionary keys and
66
+ values.
67
67
 
68
- :param d: Dictionary where each item's key corresponds to a column
69
- name, and each value is a list of values.
68
+ :param d: Dictionary where each item's key corresponds to a column name,
69
+ and each value is a list of values.
70
70
  :returns: DataFrame with a Cartesian product of each dictionary item.
71
-
72
71
  """
73
72
  index = pd.MultiIndex.from_product(d.values(), names=d.keys())
74
73
  return pd.DataFrame(index=index).reset_index()
microdf/weighted.py CHANGED
@@ -1,39 +1,48 @@
1
+ import warnings
2
+ from typing import List, Optional, Union
3
+
1
4
  import numpy as np
2
5
  import pandas as pd
3
- import warnings
4
6
 
5
7
  import microdf as mdf
6
8
 
7
9
 
8
- def weight(df, col, w=None):
10
+ def weight(
11
+ df: pd.DataFrame, col: Union[str, List[str]], w: Optional[str] = None
12
+ ) -> pd.Series:
9
13
  """Calculates the weighted value of a column in a DataFrame.
10
14
 
11
15
  :param df: A pandas DataFrame.
12
- :param col: A string indicating the column in the DataFrame to weight.
13
- Can also be a list of column strings.
16
+ :param col: A string indicating the column in the DataFrame to weight. Can
17
+ also be a list of column strings.
14
18
  :param w: Weight column.
15
19
  :returns: A pandas Series multiplying the column by its weight.
16
-
17
20
  """
18
21
  if w is None:
19
22
  return df[col]
20
23
  return df[col].multiply(df[w], axis="index")
21
24
 
22
25
 
23
- def weighted_sum(df, col, w=None, groupby=None):
26
+ def weighted_sum(
27
+ df: pd.DataFrame,
28
+ col: Union[str, List[str]],
29
+ w: Optional[str] = None,
30
+ groupby: Optional[str] = None,
31
+ ) -> pd.Series:
24
32
  """Calculates the weighted sum of a column in a DataFrame.
25
33
 
26
34
  :param df: A pandas DataFrame.
27
- :param col: A string indicating the column in the DataFrame.
28
- Can also be a list of column strings.
35
+ :param col: A string indicating the column in the DataFrame. Can also be a
36
+ list of column strings.
29
37
  :param w: Weight column.
30
38
  :param groupby: Groupby column.
31
39
  :returns: The weighted sum of a DataFrame's column.
32
-
33
40
  """
34
41
 
35
- def _weighted_sum(df, col, w):
36
- """ For weighted sum with provided weight. """
42
+ def _weighted_sum(
43
+ df: pd.DataFrame, col: Union[str, List[str]], w: Optional[str]
44
+ ) -> float:
45
+ """For weighted sum with provided weight."""
37
46
  return weight(df, col, w).sum()
38
47
 
39
48
  if groupby is None:
@@ -46,20 +55,26 @@ def weighted_sum(df, col, w=None, groupby=None):
46
55
  return df.groupby(groupby).apply(lambda x: _weighted_sum(x, col, w))
47
56
 
48
57
 
49
- def weighted_mean(df, col, w=None, groupby=None):
58
+ def weighted_mean(
59
+ df: pd.DataFrame,
60
+ col: Union[str, List[str]],
61
+ w: Optional[str] = None,
62
+ groupby: Optional[str] = None,
63
+ ) -> pd.Series:
50
64
  """Calculates the weighted mean of a column in a DataFrame.
51
65
 
52
66
  :param df: A pandas DataFrame.
53
- :param col: A string indicating the column in the DataFrame.
54
- Can also be a list of column strings.
67
+ :param col: A string indicating the column in the DataFrame. Can also be a
68
+ list of column strings.
55
69
  :param w: Weight column.
56
70
  :param groupby: Groupby column.
57
71
  :returns: The weighted mean of a DataFrame's column.
58
-
59
72
  """
60
73
 
61
- def _weighted_mean(df, col, w=None):
62
- """ For weighted mean with provided weight. """
74
+ def _weighted_mean(
75
+ df: pd.DataFrame, col: Union[str, List[str]], w: Optional[str]
76
+ ) -> float:
77
+ """For weighted mean with provided weight."""
63
78
  return weighted_sum(df, col, w) / df[w].sum()
64
79
 
65
80
  if groupby is None:
@@ -72,7 +87,9 @@ def weighted_mean(df, col, w=None, groupby=None):
72
87
  return df.groupby(groupby).apply(lambda x: _weighted_mean(x, col, w))
73
88
 
74
89
 
75
- def weighted_quantile(df: pd.DataFrame, col: str, w: str, quantiles: np.array):
90
+ def weighted_quantile(
91
+ df: pd.DataFrame, col: str, w: str, quantiles: np.array
92
+ ) -> np.array:
76
93
  """Calculates weighted quantiles of a set of values.
77
94
 
78
95
  Doesn't exactly match unweighted quantiles of stacked values.
@@ -107,18 +124,24 @@ def weighted_quantile(df: pd.DataFrame, col: str, w: str, quantiles: np.array):
107
124
  return np.interp(quantiles, weighted_quantiles, values)
108
125
 
109
126
 
110
- def weighted_median(df, col, w=None, groupby=None):
127
+ def weighted_median(
128
+ df: pd.DataFrame,
129
+ col: Union[str, List[str]],
130
+ w: Optional[str] = None,
131
+ groupby: Optional[str] = None,
132
+ ) -> pd.Series:
111
133
  """Calculates the weighted median of a column in a DataFrame.
112
134
 
113
135
  :param df: A pandas DataFrame containing Tax-Calculator data.
114
136
  :param col: A string indicating the column in the DataFrame.
115
137
  :param w: Weight column.
116
138
  :returns: The weighted median of a DataFrame's column.
117
-
118
139
  """
119
140
 
120
- def _weighted_median(df, col, w):
121
- """ For weighted median with provided weight. """
141
+ def _weighted_median(
142
+ df: pd.DataFrame, col: Union[str, List[str]], w: Optional[str]
143
+ ) -> float:
144
+ """For weighted median with provided weight."""
122
145
  return weighted_quantile(df, col, w, 0.5)
123
146
 
124
147
  if groupby is None:
@@ -131,10 +154,11 @@ def weighted_median(df, col, w=None, groupby=None):
131
154
  return df.groupby(groupby).apply(lambda x: _weighted_median(x, col, w))
132
155
 
133
156
 
134
- def add_weighted_quantiles(df, col, w):
135
- """Adds weighted quantiles of a column to a DataFrame.
136
- This will be deprecated in the next minor release. Please use
137
- MicroSeries.rank instead.
157
+ def add_weighted_quantiles(
158
+ df: pd.DataFrame, col: Union[str, List[str]], w: Optional[str] = None
159
+ ) -> None:
160
+ """Adds weighted quantiles of a column to a DataFrame. This will be
161
+ deprecated in the next minor release. Please use MicroSeries.rank instead.
138
162
 
139
163
  Adds columns for each of these types of quantiles to a DataFrame:
140
164
  * *_percentile_exact: Exact percentile.
@@ -174,7 +198,15 @@ def add_weighted_quantiles(df, col, w):
174
198
  df[col + "_quartile"] = np.ceil(df[col_pctile] / 25).astype(int)
175
199
 
176
200
 
177
- def quantile_chg(df1, df2, col1, col2, w1=None, w2=None, q=None):
201
+ def quantile_chg(
202
+ df1: pd.DataFrame,
203
+ df2: pd.DataFrame,
204
+ col1: str,
205
+ col2: str,
206
+ w1: Optional[str] = None,
207
+ w2: Optional[str] = None,
208
+ q: Optional[np.ndarray] = None,
209
+ ) -> pd.DataFrame:
178
210
  """Create table with two sets of quantiles.
179
211
 
180
212
  :param df1: DataFrame with first set of values.
@@ -184,10 +216,8 @@ def quantile_chg(df1, df2, col1, col2, w1=None, w2=None, q=None):
184
216
  :param w1: Name of weight column in df1.
185
217
  :param w2: Name of weight column in df2.
186
218
  :param q: Quantiles. Defaults to decile boundaries.
187
- :returns: DataFrame with two rows and a column for each quantile.
188
- Column labels are "xth percentile" and a label is added
189
- to the median.
190
-
219
+ :returns: DataFrame with two rows and a column for each quantile. Column
220
+ labels are "xth percentile" and a label is added to the median.
191
221
  """
192
222
  if q is None:
193
223
  q = np.arange(0.1, 1, 0.1)
@@ -0,0 +1,54 @@
1
+ Metadata-Version: 2.4
2
+ Name: microdf-python
3
+ Version: 0.4.6
4
+ Summary: Survey microdata as DataFrames
5
+ Author-email: Max Ghenis <max@ubicenter.org>
6
+ License: MIT
7
+ Requires-Python: >=3.9
8
+ Description-Content-Type: text/markdown
9
+ License-File: LICENSE
10
+ Requires-Dist: numpy
11
+ Requires-Dist: pandas
12
+ Provides-Extra: dev
13
+ Requires-Dist: codecov; extra == "dev"
14
+ Requires-Dist: flake8; extra == "dev"
15
+ Requires-Dist: flake8-pyproject; extra == "dev"
16
+ Requires-Dist: black; extra == "dev"
17
+ Requires-Dist: docformatter; extra == "dev"
18
+ Requires-Dist: isort; extra == "dev"
19
+ Requires-Dist: linecheck; extra == "dev"
20
+ Requires-Dist: matplotlib; extra == "dev"
21
+ Requires-Dist: pip; extra == "dev"
22
+ Requires-Dist: pytest; extra == "dev"
23
+ Requires-Dist: pytest-cov; extra == "dev"
24
+ Requires-Dist: seaborn; extra == "dev"
25
+ Requires-Dist: setuptools; extra == "dev"
26
+ Requires-Dist: build; extra == "dev"
27
+ Provides-Extra: docs
28
+ Requires-Dist: jupyter_book; extra == "docs"
29
+ Provides-Extra: taxcalc
30
+ Requires-Dist: taxcalc; extra == "taxcalc"
31
+ Provides-Extra: charts
32
+ Requires-Dist: seaborn; extra == "charts"
33
+ Requires-Dist: matplotlib; extra == "charts"
34
+ Requires-Dist: matplotlib-label-lines; extra == "charts"
35
+ Dynamic: license-file
36
+
37
+ [![Build](https://github.com/PSLmodels/microdf/workflows/Build%20and%20test%20[Python%203.7,%203.8,%203.9]/badge.svg)](https://github.com/PSLmodels/microdf/actions?query=workflow%3A%22Build+and+test+%5BPython+3.7%2C+3.8%2C+3.9%5D%22)
38
+ [![Codecov](https://codecov.io/gh/PSLmodels/microdf/branch/master/graph/badge.svg)](https://codecov.io/gh/PSLmodels/microdf)
39
+
40
+ # microdf
41
+ Analysis tools for working with survey microdata as DataFrames.
42
+
43
+ *Disclaimer: `MicroSeries` and `MicroDataFrame` are experimental features and may not consider weights after performing some operations. See open issues.*
44
+
45
+ ## Installation
46
+ Install with:
47
+
48
+ pip install git+git://github.com/PSLmodels/microdf.git
49
+
50
+ ## Questions
51
+ Contact the maintainer, Max Ghenis (mghenis@gmail.com).
52
+
53
+ ## Citation
54
+ You may cite the source of your analysis as "microdf release #.#.#, author's calculations."
@@ -0,0 +1,37 @@
1
+ microdf/__init__.py,sha256=rLo3znQsUATrY1LynbfAKauIEIkaqavpyHUhKupecWE,3463
2
+ microdf/_optional.py,sha256=9y0dpWzljfbbZY5AzfaOiSXHZuBKJsSmVitT1czmGuo,2831
3
+ microdf/agg.py,sha256=rMUm6np-4Qwd8mwpSya_c7VMuon4KA9H0rLyOGTRLa8,3152
4
+ microdf/chart_utils.py,sha256=a47NXCWDr1pyeXQHmvUzYFJtLomg2tHn_dw0fsS3rsg,1102
5
+ microdf/charts.py,sha256=ZDC30-msjqJTEHsXD3VcpG39Jew5Nrqh4WSDN8vuFKE,2099
6
+ microdf/concat.py,sha256=wgpADTy7BobmJH8QiPZDFtFunPqqkWG21yoxDOnlDRc,951
7
+ microdf/constants.py,sha256=CJ1kRSvYQmxkEf-4-Oxk5Y4ZqGPegpk8HfoF_LR6qSI,960
8
+ microdf/custom_taxes.py,sha256=6igJI7YkYFKbxHRVN44Fs9CnUcE6chEgbH01gsfDLtM,6504
9
+ microdf/generic.py,sha256=FbqpiBlQEo_YgwTePmafUFzglXPLQceulg7SW8zbLxI,33342
10
+ microdf/income_measures.py,sha256=5Spu9Z4axe3CT43x-bWtog2rFxlwwFX5InW6S39WWFs,2141
11
+ microdf/inequality.py,sha256=ErLJwSy_l2f7h8MV04C9lMbW4LetffLsJE4NaUnFdu8,7064
12
+ microdf/io.py,sha256=cTaMt1gT9E3Jy6rB0qycrAVcsvhcnUPBVKm_wQKSeSs,872
13
+ microdf/poverty.py,sha256=wBy5o79LtBxOV88fMGr-0Bzi8AqcHdW9qNQ2q4GJ8qo,4604
14
+ microdf/style.py,sha256=c1XvwBwv9iBpoTh_4jn-je8qm2sJYh4PLN4oYnKP9XM,1726
15
+ microdf/tax.py,sha256=XMbSJtwF0amgnAlRB39yewgrYR-PwcRcv4e8oqB-m-M,3126
16
+ microdf/taxcalc.py,sha256=3JSZATCqqCFUXDT_CaJZfLwV4J3gu8PVRZ9T8zdMPqg,5442
17
+ microdf/ubi.py,sha256=5ZsVbZVVKS-p-e3PEVmLeQPatbwXi9YEvw01BjZ62Y0,1716
18
+ microdf/utils.py,sha256=ow37r7SOKkgfAyiYBlcQGaRlo2aqOp7jb8ZVakhY0BI,1949
19
+ microdf/weighted.py,sha256=XjyRkLimTebpa6vJuBOuuj5pWLB3dA_BKjLB_36J1m8,8101
20
+ microdf/tests/conftest.py,sha256=u-EMyX1-u_nM-YO0RJYCzYHQDXxUI2WQE6GkyJlErqg,150
21
+ microdf/tests/test_compare.py,sha256=BKoOO6IXQgEwmqvUW26LRVqVWkB5tbe-0ZIowhKHB6Q,1734
22
+ microdf/tests/test_decile_rank.py,sha256=GFkYAY6FMdP9v1uNEhQ_TMTQ38PNPkFSnBDc0Khguns,3421
23
+ microdf/tests/test_generic.py,sha256=8wY2SBm7C7v7k-09vEIHY32k-QrDH5tzm8PkRl0R3CA,7347
24
+ microdf/tests/test_inequality.py,sha256=XMxsrqJ6kcX9XW4NfaDZTEy46ANLYJYGGATIl8IfP6k,595
25
+ microdf/tests/test_io.py,sha256=Rdd2nale5-5hStzQshTXLNk7YJm2ErfFrcXSU57uec0,293
26
+ microdf/tests/test_optional_dependency.py,sha256=2B8MLac-Sgg40lO6y77gYydrOK6i8wShFovvxa-xEZs,1483
27
+ microdf/tests/test_poverty.py,sha256=JcfbvYVOMGJ9N7QtPKpwco-6X3drjHweFInQcm-lGmw,2116
28
+ microdf/tests/test_quantile_chg.py,sha256=Eh6FrVKYc-k1ICFFjb-zUp9YstmcloXX11sj45k-J6Y,375
29
+ microdf/tests/test_tax.py,sha256=DUSfT6ePXBTXqMvVZN9mhpHr0-Q5ahltqUefCq9qE7E,1790
30
+ microdf/tests/test_taxcalc.py,sha256=YBycHzT-2EMn2kIopI5yfsfDdGcr6PWG_nlexe4_Fd0,482
31
+ microdf/tests/test_utils.py,sha256=ViBDGZ-0B4KD474f19pb31zHPRjfBebTo_uSRzT2GrU,924
32
+ microdf/tests/test_weighted.py,sha256=-MzobQ3dYRDf-an3kSHJWKEG7HHKCLhwmxQYJYTuyVk,2021
33
+ microdf_python-0.4.6.dist-info/licenses/LICENSE,sha256=uPs-ASYnzlldpf2z8jeRgQFeEH3FLhSuX0rw0OKWoDU,1067
34
+ microdf_python-0.4.6.dist-info/METADATA,sha256=MkeGz0ls3SgcLkOcDJ3ZS9zTZ2DSBHTVVg3F0qA2dzY,2042
35
+ microdf_python-0.4.6.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
36
+ microdf_python-0.4.6.dist-info/top_level.txt,sha256=T2WFPTygQQMdS3GF8YpZ12DKfMGrspbZ3r7z-e3KfiM,8
37
+ microdf_python-0.4.6.dist-info/RECORD,,
@@ -1,25 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: microdf-python
3
- Version: 0.4.4
4
- Summary: Survey microdata as DataFrames.
5
- Home-page: http://github.com/PSLmodels/microdf
6
- Author: Max Ghenis
7
- Author-email: max@ubicenter.org
8
- License: MIT
9
- License-File: LICENSE
10
- Requires-Dist: numpy
11
- Requires-Dist: pandas
12
- Provides-Extra: taxcalc
13
- Requires-Dist: taxcalc; extra == "taxcalc"
14
- Provides-Extra: charts
15
- Requires-Dist: seaborn; extra == "charts"
16
- Requires-Dist: matplotlib; extra == "charts"
17
- Requires-Dist: matplotlib-label-lines; extra == "charts"
18
- Dynamic: author
19
- Dynamic: author-email
20
- Dynamic: home-page
21
- Dynamic: license
22
- Dynamic: license-file
23
- Dynamic: provides-extra
24
- Dynamic: requires-dist
25
- Dynamic: summary
@@ -1,24 +0,0 @@
1
- microdf/__init__.py,sha256=5YubH2TcCDG-e89pM22C0JhcRZHGcHwSnHHFo5GSPrQ,3463
2
- microdf/_optional.py,sha256=pZR05BhAsE744HHp2YTydheX_ovqeLA1gvQewLnwXpY,2738
3
- microdf/agg.py,sha256=j6CHm-ZgzdZsYk3c2i07DrmF6SS-woiavB-kgsLz3aE,3127
4
- microdf/chart_utils.py,sha256=OK2QWBrUUktSy_oEDKXqHIjwQzRSV00MS4PBvwBK2iw,934
5
- microdf/charts.py,sha256=MlsZl3TGAOFrTtN4jF4FMDcNQZjFdY6URg_DXNdw3nQ,1997
6
- microdf/concat.py,sha256=eIwf-LOTi8CUqh913IKgTbXXg6i1_c3ghfJuSL13S58,886
7
- microdf/constants.py,sha256=CJ1kRSvYQmxkEf-4-Oxk5Y4ZqGPegpk8HfoF_LR6qSI,960
8
- microdf/custom_taxes.py,sha256=2tOfIMP-vtaBnvwYhk8K-BZohoRLyPWKqhjErLujc7s,6049
9
- microdf/generic.py,sha256=y_FFXy6xuS4lkFPgx4gnxVbT5CEzNnO1JHuSbBcaBiI,28171
10
- microdf/income_measures.py,sha256=ya2iAGDdJmrUMHRN2wd8MhE7hFoyFa8QDZPYaKgdv_w,2045
11
- microdf/inequality.py,sha256=M4SBe5V7YwHO-63VQk0CU81VB4rZz0u0waW0oE5nfA0,5910
12
- microdf/io.py,sha256=8jBVUqQ7kzOhgHDhHDYY7WK6t6okYc5LoloJxh8JSA0,888
13
- microdf/poverty.py,sha256=GDCTM7doJSFd4jJvSSX5BLVUh6Sd7NGNUgXWKS69M6A,4615
14
- microdf/style.py,sha256=O5ebjIWNsq35UbOclOj4tzY_Evy2S-SZQapqcTfEo2o,1718
15
- microdf/tax.py,sha256=sErlnCQLTPTTDL61T3kDYGYEytIxJFf-m64-b-K2R5w,3129
16
- microdf/taxcalc.py,sha256=5Ixl_hTfz8iQXKRnRuj5HZm2Elun6TgU-o3mZMgjPkw,5056
17
- microdf/ubi.py,sha256=QG9gs6rOnHCIPeSArwemVe4uDsd5oswnJ3wmo4MxDGI,1581
18
- microdf/utils.py,sha256=9QFu7pZd9y09RvCebERM4SN_DedBogEE2oK-KG2YxFc,1795
19
- microdf/weighted.py,sha256=hekD_BitDTK9Q6HqzbWKhZQPFpfCfvQxcOntqdejycw,7246
20
- microdf_python-0.4.4.dist-info/licenses/LICENSE,sha256=uPs-ASYnzlldpf2z8jeRgQFeEH3FLhSuX0rw0OKWoDU,1067
21
- microdf_python-0.4.4.dist-info/METADATA,sha256=6r_dNH14oaP-WHhUy34HeIbyZYGUg0-lvzjc_BYZFqk,669
22
- microdf_python-0.4.4.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
23
- microdf_python-0.4.4.dist-info/top_level.txt,sha256=T2WFPTygQQMdS3GF8YpZ12DKfMGrspbZ3r7z-e3KfiM,8
24
- microdf_python-0.4.4.dist-info/RECORD,,