cudf.from_pandas#
- cudf.from_pandas(obj, nan_as_null=_NoDefault.no_default)[source]#
Convert certain Pandas objects into the cudf equivalent.
Supports DataFrame, Series, Index, or MultiIndex.
- Returns:
- DataFrame/Series/Index/MultiIndex
Return type depends on the passed input.
- Raises:
- TypeError for invalid input type.
Examples
>>> import cudf >>> import pandas as pd >>> data = [[0, 1], [1, 2], [3, 4]] >>> pdf = pd.DataFrame(data, columns=['a', 'b'], dtype=int) >>> pdf a b 0 0 1 1 1 2 2 3 4 >>> gdf = cudf.from_pandas(pdf) >>> gdf a b 0 0 1 1 1 2 2 3 4 >>> type(gdf) <class 'cudf.core.dataframe.DataFrame'> >>> type(pdf) <class 'pandas.core.frame.DataFrame'>
Converting a Pandas Series to cuDF Series:
>>> psr = pd.Series(['a', 'b', 'c', 'd'], name='apple', dtype='str') >>> psr 0 a 1 b 2 c 3 d Name: apple, dtype: object >>> gsr = cudf.from_pandas(psr) >>> gsr 0 a 1 b 2 c 3 d Name: apple, dtype: object >>> type(gsr) <class 'cudf.core.series.Series'> >>> type(psr) <class 'pandas.core.series.Series'>
Converting a Pandas Index to cuDF Index:
>>> pidx = pd.Index([1, 2, 10, 20]) >>> pidx Index([1, 2, 10, 20], dtype='int64') >>> gidx = cudf.from_pandas(pidx) >>> gidx Index([1, 2, 10, 20], dtype='int64') >>> type(gidx) <class 'cudf.core.index.Index'> >>> type(pidx) <class 'pandas.core.indexes.base.Index'>
Converting a Pandas MultiIndex to cuDF MultiIndex:
>>> pmidx = pd.MultiIndex( ... levels=[[1, 3, 4, 5], [1, 2, 5]], ... codes=[[0, 0, 1, 2, 3], [0, 2, 1, 1, 0]], ... names=["x", "y"], ... ) >>> pmidx MultiIndex([(1, 1), (1, 5), (3, 2), (4, 2), (5, 1)], names=['x', 'y']) >>> gmidx = cudf.from_pandas(pmidx) >>> gmidx MultiIndex([(1, 1), (1, 5), (3, 2), (4, 2), (5, 1)], names=['x', 'y']) >>> type(gmidx) <class 'cudf.core.multiindex.MultiIndex'> >>> type(pmidx) <class 'pandas.core.indexes.multi.MultiIndex'>