feilian 1.2.2__tar.gz → 1.2.4__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.

Potentially problematic release.


This version of feilian might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: feilian
3
- Version: 1.2.2
3
+ Version: 1.2.4
4
4
  Summary: General data processing tool.
5
5
  Author-email: darkpeath <darkpeath@gmail.com>
6
6
  Project-URL: Homepage, https://github.com/darkpeath/feilian
@@ -1,5 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  # file generated by setuptools_scm
3
3
  # don't change, don't track in version control
4
- VERSION = (1, 2, 2)
5
- __version__ = '1.2.2'
4
+ VERSION = (1, 2, 4)
5
+ __version__ = '1.2.4'
@@ -25,9 +25,18 @@ if pd_version[0] < 1 or (pd_version[0] == 1 and pd_version[1] < 5):
25
25
  FILE_FORMAT = Literal['csv', 'tsv', 'json', 'xlsx', 'parquet']
26
26
  COMPRESSION_FORMAT = Literal[None, 'infer', 'snappy', 'gzip', 'brotli', 'bz2', 'zip', 'xz']
27
27
 
28
+ def _drop_na_values(data: Union[pd.DataFrame, Dict[str, pd.DataFrame]], axis: Literal['columns', 'rows']):
29
+ if isinstance(data, pd.DataFrame):
30
+ data.dropna(axis=axis, how='all', inplace=True)
31
+ else:
32
+ assert isinstance(data, dict)
33
+ for df in data.values():
34
+ df.dropna(axis=axis, how='all', inplace=True)
35
+
28
36
  def read_dataframe(file: str, *args, sheet_name=0,
29
37
  file_format: FILE_FORMAT = None,
30
38
  jsonl=False, dtype: type = None,
39
+ drop_na_columns=False, drop_na_rows=False,
31
40
  **kwargs) -> Union[pd.DataFrame, Dict[str, pd.DataFrame]]:
32
41
  """
33
42
  read file as pandas `DataFrame`
@@ -37,6 +46,8 @@ def read_dataframe(file: str, *args, sheet_name=0,
37
46
  :param file_format: csv, tsv, json ,xlsx, parquet
38
47
  :param jsonl: jsonl format or not, only used in json format
39
48
  :param dtype: `dtype` for `pd.read_xx()`
49
+ :param drop_na_columns: drop column if all values of the column is na
50
+ :param drop_na_rows: drop row if all values of the row is na
40
51
  :param kwargs: extra kwargs for `pd.read_xx()`
41
52
  """
42
53
  # decide the file format
@@ -61,16 +72,23 @@ def read_dataframe(file: str, *args, sheet_name=0,
61
72
  jsonl = True
62
73
 
63
74
  if file_format == 'csv':
64
- return pd.read_csv(file, *args, dtype=dtype, **kwargs)
75
+ df = pd.read_csv(file, *args, dtype=dtype, **kwargs)
65
76
  elif file_format == 'xlsx':
66
- return pd.read_excel(file, *args, sheet_name=sheet_name, dtype=dtype, **kwargs)
77
+ df = pd.read_excel(file, *args, sheet_name=sheet_name, dtype=dtype, **kwargs)
67
78
  elif file_format == 'json':
68
- return pd.read_json(file, *args, lines=jsonl, dtype=dtype, **kwargs)
79
+ df = pd.read_json(file, *args, lines=jsonl, dtype=dtype, **kwargs)
69
80
  elif file_format == 'parquet':
70
- return pd.read_parquet(file, *args, **kwargs)
81
+ df = pd.read_parquet(file, *args, **kwargs)
71
82
  else:
72
83
  raise IOError(f"Unknown file format: {file}")
73
84
 
85
+ if drop_na_columns:
86
+ _drop_na_values(df, axis='columns')
87
+ if drop_na_rows:
88
+ _drop_na_values(df, axis='rows')
89
+
90
+ return df
91
+
74
92
  def save_dataframe(file: Union[str, 'pd.WriteBuffer[bytes]', 'pd.WriteBuffer[str]'],
75
93
  df: Union[pd.DataFrame, Iterable[Union[pd.Series, Dict[str, Any]]]],
76
94
  *args, sheet_name='Sheet1',
@@ -1,8 +1,14 @@
1
1
  import abc
2
2
  import tqdm
3
3
  import pandas as pd
4
- from typing import Any, Dict, Hashable, List, Tuple, Union, Iterable
5
- from .dataframe import read_dataframe, save_dataframe
4
+ from typing import (
5
+ Any, Dict, Hashable, List,
6
+ Tuple, Union, Iterable, Optional,
7
+ )
8
+ from .dataframe import (
9
+ read_dataframe,
10
+ save_dataframe,
11
+ )
6
12
 
7
13
  class BaseProcessor(abc.ABC):
8
14
  """
@@ -55,11 +61,13 @@ class BaseProcessor(abc.ABC):
55
61
  self.save_result(output_path or input_path, result)
56
62
 
57
63
  class DataframeProcessor(BaseProcessor, abc.ABC):
58
- def __init__(self, input_dtype=None, progress=False, read_args: Dict[str, Any] = None):
64
+ def __init__(self, input_dtype=None, progress=False, read_args: Dict[str, Any] = None,
65
+ write_args: Dict[str, Any] = None):
59
66
  self.progress = progress
60
67
  self.read_args = read_args or {}
61
68
  if input_dtype is not None:
62
69
  self.read_args['dtype'] = input_dtype
70
+ self.write_args = write_args or {}
63
71
 
64
72
  def read_single_file(self, filepath: str) -> pd.DataFrame:
65
73
  return read_dataframe(filepath, **self.read_args)
@@ -71,12 +79,13 @@ class DataframeProcessor(BaseProcessor, abc.ABC):
71
79
  return super().read_data(filepath)
72
80
 
73
81
  def save_result(self, filepath: str, result: pd.DataFrame):
74
- save_dataframe(filepath, result)
82
+ save_dataframe(filepath, result, **self.write_args)
75
83
 
76
84
  @abc.abstractmethod
77
- def process_row(self, i: Hashable, row: pd.Series) -> Dict[str, Any]:
85
+ def process_row(self, i: Hashable, row: pd.Series) -> Optional[Dict[str, Any]]:
78
86
  """
79
87
  Process a single row of data.
88
+ :return: if `None`, ignore this row
80
89
  """
81
90
 
82
91
  def process(self, data: pd.DataFrame) -> pd.DataFrame:
@@ -84,6 +93,7 @@ class DataframeProcessor(BaseProcessor, abc.ABC):
84
93
  if self.progress:
85
94
  desc = "process" if self.progress is True else self.progress
86
95
  bar = tqdm.tqdm(bar, total=len(data), desc=desc)
87
- res = [self.process_row(i, row) for i, row in bar]
96
+ res = (self.process_row(i, row) for i, row in bar)
97
+ res = (x for x in res if x is not None)
88
98
  return pd.DataFrame(res)
89
99
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: feilian
3
- Version: 1.2.2
3
+ Version: 1.2.4
4
4
  Summary: General data processing tool.
5
5
  Author-email: darkpeath <darkpeath@gmail.com>
6
6
  Project-URL: Homepage, https://github.com/darkpeath/feilian
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes