@@ -36,12 +36,20 @@ large, mostly NA ``DataFrame``:
36
36
df = pd.DataFrame(np.random.randn(10000 , 4 ))
37
37
df.iloc[:9998 ] = np.nan
38
38
sdf = df.astype(pd.SparseDtype(" float" , np.nan))
39
- sdf
39
+ sdf.head()
40
+ sdf.dtypes
40
41
sdf.sparse.density
41
42
42
43
As you can see, the density (% of values that have not been "compressed") is
43
44
extremely low. This sparse object takes up much less memory on disk (pickled)
44
- and in the Python interpreter. Functionally, their behavior should be nearly
45
+ and in the Python interpreter.
46
+
47
+ .. ipython :: python
48
+
49
+ print (' dense : {:0.2f } bytes' .format(df.memory_usage().sum() / 1e3 ))
50
+ print (' sparse: {:0.2f } bytes' .format(sdf.memory_usage().sum() / 1e3 ))
51
+
52
+ Functionally, their behavior should be nearly
45
53
identical to their dense counterparts.
46
54
47
55
.. _sparse.array :
@@ -73,6 +81,12 @@ The :attr:`SparseArray.dtype` property stores two pieces of information
73
81
1. The dtype of the non-sparse values
74
82
2. The scalar fill value
75
83
84
+
85
+ .. ipython :: python
86
+
87
+ sparr.dtype
88
+
89
+
76
90
A :class: `SparseDtype ` may be constructed by passing each of these
77
91
78
92
.. ipython :: python
@@ -118,7 +132,7 @@ class itself for creating a Series with sparse data from a scipy COO matrix with
118
132
.. versionadded :: 0.25.0
119
133
120
134
A ``.sparse `` accessor has been added for :class: `DataFrame ` as well.
121
- See :ref: `api.dataframe .sparse ` for more.
135
+ See :ref: `api.frame .sparse ` for more.
122
136
123
137
.. _sparse.calculation :
124
138
@@ -160,11 +174,6 @@ This section provides some guidance on migrating your code to the new style. As
160
174
you can use the python warnings module to control warnings. But we recommend modifying
161
175
your code, rather than ignoring the warning.
162
176
163
- **General Differences **
164
-
165
- In a SparseDataFrame, *all * columns were sparse. A :class: `DataFrame ` can have a mixture of
166
- sparse and dense columns.
167
-
168
177
**Construction **
169
178
170
179
From an array-like, use the regular :class: `Series ` or
@@ -195,7 +204,7 @@ From a SciPy sparse matrix, use :meth:`DataFrame.sparse.from_spmatrix`,
195
204
from scipy import sparse
196
205
mat = sparse.eye(3 )
197
206
df = pd.DataFrame.sparse.from_spmatrix(mat, columns = [' A' , ' B' , ' C' ])
198
- df
207
+ df.dtypes
199
208
200
209
**Conversion **
201
210
@@ -205,7 +214,6 @@ From sparse to dense, use the ``.sparse`` accessors
205
214
206
215
df.sparse.to_dense()
207
216
df.sparse.to_coo()
208
- df[' A' ]
209
217
210
218
From dense to sparse, use :meth: `DataFrame.astype ` with a :class: `SparseDtype `.
211
219
@@ -223,6 +231,30 @@ Sparse-specific properties, like ``density``, are available on the ``.sparse`` a
223
231
224
232
df.sparse.density
225
233
234
+ **General Differences **
235
+
236
+ In a SparseDataFrame, *all * columns were sparse. A :class: `DataFrame ` can have a mixture of
237
+ sparse and dense columns. As a consequence, assigning new columns to a DataFrame with sparse
238
+ values will not automatically convert the input to be sparse.
239
+
240
+ .. code-block ::
241
+
242
+ # Previous Way
243
+ df = pd.SparseDataFrame({"A": [0, 1]})
244
+ df['B'] = [0, 0] # implicitly becomes Sparse
245
+ df['B'].dtype
246
+ Sparse[int64, nan]
247
+
248
+ Instead, you'll need to ensure that the values being assigned are sparse
249
+
250
+ .. ipython :: python
251
+
252
+ df = pd.DataFrame({" A" : pd.SparseArray([0 , 1 ])})
253
+ df[' B' ] = [0 , 0 ] # remains dense
254
+ df[' B' ].dtype
255
+ df[' B' ] = pd.SparseArray([0 , 0 ])
256
+ df[' B' ].dtype
257
+
226
258
The ``SparseDataFrame.default_kind `` and ``SparseDataFrame.default_fill_value `` attributes
227
259
have no replacement.
228
260
0 commit comments