Skip to content

Commit d1bc16e

Browse files
committed
Add Series.name and Series.from_pandas
1 parent 46cb1d4 commit d1bc16e

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

pygdf/dataframe.py

+1
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ def add_column(self, name, data, forceindex=False):
423423
raise NameError('duplicated column name {!r}'.format(name))
424424

425425
series = self._prepare_series_for_add(data, forceindex=forceindex)
426+
series.name = name
426427
self._cols[name] = series
427428

428429
def drop_column(self, name):

pygdf/series.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from numbers import Number
77

88
import numpy as np
9+
import pandas as pd
910

1011
from . import cudautils, formatting
1112
from .buffer import Buffer
@@ -59,9 +60,20 @@ def from_masked_array(cls, data, mask, null_count=None):
5960
return cls(data=col)
6061

6162
def __init__(self, data, index=None):
63+
name = None
64+
if isinstance(data, pd.Series):
65+
from .dataframe import DataFrame
66+
name = data.name
67+
data = data.to_frame()
68+
data.columns = ['x']
69+
data = DataFrame.from_pandas(data)
70+
data = data['x']
71+
data.name = name
6272
if isinstance(data, Series):
6373
index = data._index
74+
name = data.name
6475
data = data._column
76+
6577
if not isinstance(data, columnops.TypedColumnBase):
6678
data = columnops.as_column(data)
6779

@@ -71,6 +83,11 @@ def __init__(self, data, index=None):
7183
assert isinstance(data, columnops.TypedColumnBase)
7284
self._column = data
7385
self._index = RangeIndex(len(data)) if index is None else index
86+
self.name = name
87+
88+
@classmethod
89+
def from_pandas(cls, s):
90+
return cls(s)
7491

7592
def serialize(self, serialize):
7693
header = {}
@@ -448,7 +465,9 @@ def to_gpu_array(self, fillna=None):
448465
def to_pandas(self, index=True):
449466
if index is True:
450467
index = self.index.to_pandas()
451-
return self._column.to_pandas(index=index)
468+
s = self._column.to_pandas(index=index)
469+
s.name = self.name
470+
return s
452471

453472
@property
454473
def data(self):

pygdf/tests/test_dataframe.py

+20
Original file line numberDiff line numberDiff line change
@@ -670,3 +670,23 @@ def do_slice(x):
670670
got = do_slice(gdf).to_pandas()
671671

672672
pd.testing.assert_frame_equal(expect, got)
673+
674+
675+
def test_from_pandas():
676+
df = pd.DataFrame({'x': [1, 2, 3]}, index=[4., 5., 6.])
677+
gdf = gd.DataFrame.from_pandas(df)
678+
assert isinstance(gdf, gd.DataFrame)
679+
680+
pd.testing.assert_frame_equal(df, gdf.to_pandas())
681+
682+
s = df.x
683+
gs = gd.Series.from_pandas(s)
684+
assert isinstance(gs, gd.Series)
685+
686+
pd.testing.assert_series_equal(s, gs.to_pandas())
687+
688+
689+
def test_series_name():
690+
df = pd.DataFrame({'x': [1, 2, 3]}, index=[4., 5., 6.])
691+
gdf = gd.DataFrame.from_pandas(df)
692+
assert gdf['x'].name == 'x'

0 commit comments

Comments
 (0)