Skip to content

Commit

Permalink
Draft solution to nithinmurali#423
Browse files Browse the repository at this point in the history
When include_tailing_empty=False, now returns a DataFrame just as wide
as necessary to accommodate the header/data. Issues a warning if
has_header=True and >=1 column name is an empty string.
  • Loading branch information
RussianImperialScott committed Feb 1, 2021
1 parent 3d54d3c commit eaa54ef
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions pygsheets/worksheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import warnings
import logging

import pdb

from pygsheets.cell import Cell
from pygsheets.datarange import DataRange
from pygsheets.address import GridRange, Address
Expand Down Expand Up @@ -1416,7 +1418,7 @@ def get_as_df(self, has_header=True, index_column=None, start=None, end=None, nu
"""
if not self._linked: return False

include_tailing_empty = True if has_header else kwargs.get('include_tailing_empty', False)
include_tailing_empty = kwargs.get('include_tailing_empty', False)
include_tailing_empty_rows = kwargs.get('include_tailing_empty_rows', False)
index_column = index_column or kwargs.get('index_colum', None)

Expand All @@ -1431,13 +1433,18 @@ def get_as_df(self, has_header=True, index_column=None, start=None, end=None, nu
else:
values = self.get_all_values(returnas='matrix', include_tailing_empty=include_tailing_empty,
value_render=value_render, include_tailing_empty_rows=include_tailing_empty_rows)

max_row = max(len(row) for row in values)
values = [row + [empty_value] * (max_row - len(row)) for row in values]

if numerize:
values = [numericise_all(row, empty_value) for row in values]

if has_header:
keys = values[0]
values = [row[:len(keys)] for row in values[1:]]
values = values[1:]
if any(key == '' for key in keys):
warnings.warn('At least one column name in the data frame is an empty string. If this is a concern, please specify include_tailing_empty=False and/or ensure that each column containing data has a name.')
df = pd.DataFrame(values, columns=keys)
else:
df = pd.DataFrame(values)
Expand Down

0 comments on commit eaa54ef

Please sign in to comment.