Skip to content

Commit

Permalink
Merge pull request #634 from AK-ayush/num_cols
Browse files Browse the repository at this point in the history
[REVIEW] Enable DataFrame creation from_pandas with numeric column names.
  • Loading branch information
harrism committed Jan 9, 2019
2 parents b2586d8 + f6851ce commit b74591b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -49,6 +49,7 @@
- PR #612 Prevent an exception from occuring with true division on integer series.
- PR #630 Fix deprecation warning for `pd.core.common.is_categorical_dtype`
- PR #622 Fix Series.append() behaviour when appending values with different numeric dtype
- PR #634 Fix create `DataFrame.from_pandas()` with numeric column names


# cuDF 0.4.0 (05 Dec 2018)
Expand Down
7 changes: 4 additions & 3 deletions python/cudf/dataframe/dataframe.py
Expand Up @@ -6,6 +6,7 @@
import random
from collections import OrderedDict
import warnings
import numbers

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -183,7 +184,7 @@ def __getattr__(self, key):

def __getitem__(self, arg):
"""
If *arg* is a ``str``, return the column Series.
If *arg* is a ``str`` or ``int`` type, return the column Series.
If *arg* is a ``slice``, return a new DataFrame with all columns
sliced to the specified range.
If *arg* is an ``array`` containing column names, return a new
Expand Down Expand Up @@ -217,7 +218,7 @@ def __getitem__(self, arg):
>>> df[[True, False, True, False]] # mask the entire dataframe,
# returning the rows specified in the boolean mask
"""
if isinstance(arg, str) or isinstance(arg, int):
if isinstance(arg, str) or isinstance(arg, numbers.Integral):
s = self._cols[arg]
s.name = arg
return s
Expand All @@ -238,7 +239,7 @@ def __getitem__(self, arg):
return df
else:
msg = "__getitem__ on type {!r} is not supported"
raise TypeError(msg.format(arg))
raise TypeError(msg.format(type(arg)))

def __setitem__(self, name, col):
"""Add/set column by *name*
Expand Down
3 changes: 2 additions & 1 deletion python/cudf/formatting.py
Expand Up @@ -3,6 +3,7 @@
"""
Define how data are formatted
"""
import numbers


def format(index, cols, show_headers=True, more_cols=0, more_rows=0,
Expand Down Expand Up @@ -49,7 +50,7 @@ def format(index, cols, show_headers=True, more_cols=0, more_rows=0,
# compute column widths
widths = {}
for k, vs in cols.items():
if isinstance(k, int):
if isinstance(k, numbers.Integral):
widths[k] = min_width
else:
widths[k] = max(len(k), max(map(len, vs), default=0), min_width)
Expand Down
7 changes: 7 additions & 0 deletions python/cudf/tests/test_dataframe.py
Expand Up @@ -187,6 +187,13 @@ def test_dataframe_column_name_indexing():
for idx in combinations(pdf.columns, i):
assert(pdf[list(idx)].equals(df[list(idx)].to_pandas()))

# test for only numeric columns
df = pd.DataFrame()
for i in range(0, 10):
df[i] = range(nelem)
gdf = DataFrame.from_pandas(df)
assert_eq(gdf, df)


def test_dataframe_drop_method():
df = DataFrame()
Expand Down

0 comments on commit b74591b

Please sign in to comment.