Skip to content

Commit

Permalink
Fix: honour --head when --columns is provided. (#23)
Browse files Browse the repository at this point in the history
This fix ensures that only the number of rows specified by the client
will be output when the --columns argument is supplied (fixes
#22).
  • Loading branch information
exaspace authored Dec 25, 2021
1 parent 1c70a07 commit 32390a1
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion parquet_tools/commands/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ def _execute(df: pd.DataFrame, head: int, columns: list) -> None:
# head
df_head: pd.DataFrame = df.head(head) if head > 0 else df
# select columns
df_select: pd.DataFrame = df[columns] if len(columns) else df_head
df_select: pd.DataFrame = df_head[columns] if len(columns) else df_head
print(df_select.to_csv(index=None))
2 changes: 1 addition & 1 deletion parquet_tools/commands/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,5 @@ def _execute(df: pd.DataFrame, format: str, head: int, columns: list) -> None:
# head
df_head: pd.DataFrame = df.head(head) if head > 0 else df
# select columns
df_select: pd.DataFrame = df[columns] if len(columns) else df_head
df_select: pd.DataFrame = df_head[columns] if len(columns) else df_head
print(tabulate(df_select, df_select.columns, tablefmt=format, showindex=False))
7 changes: 5 additions & 2 deletions tests/test_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,12 @@ def test_excute(capfd, parquet_file):
'two': ['foo', 'bar', 'baz'],
'three': [True, False, True]}
),
head=-1,
columns=[]
head=2,
columns=['one', 'three']
)
out, err = capfd.readouterr()
assert out is not None
assert err == ''
assert 'foo' not in out, 'Column two should not be output'
assert '2.5' not in out, 'Row 3 should not be output'

8 changes: 6 additions & 2 deletions tests/test_show.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,13 @@ def test_excute(capfd, parquet_file):
'three': [True, False, True]}
),
format='psql',
head=-1,
columns=[]
head=2,
columns=['one', 'three']
)
out, err = capfd.readouterr()

assert out is not None
assert err == ''
assert 'foo' not in out, 'Column two should not be output'
assert '2.5' not in out, 'Row 3 should not be output'

0 comments on commit 32390a1

Please sign in to comment.