Skip to content

Commit 6ec026c

Browse files
committed
Merge branch 'from-records-fix-3682' of https://github.com/cpcloud/pandas into cpcloud-from-records-fix-3682
Conflicts: doc/source/v0.11.1.txt
2 parents d4560fb + cec9401 commit 6ec026c

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

RELEASE.rst

+2
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ pandas 0.11.1
173173
- ``read_html()`` now only allows a single backend: ``html5lib`` (GH3616_)
174174
- ``convert_objects`` with ``convert_dates='coerce'`` was parsing some single-letter strings
175175
into today's date
176+
- ``DataFrame.from_records`` did not accept empty recarrays (GH3682_)
176177

177178
.. _GH3164: https://github.com/pydata/pandas/issues/3164
178179
.. _GH2786: https://github.com/pydata/pandas/issues/2786
@@ -246,6 +247,7 @@ pandas 0.11.1
246247
.. _GH3582: https://github.com/pydata/pandas/issues/3582
247248
.. _GH3676: https://github.com/pydata/pandas/issues/3676
248249
.. _GH3675: https://github.com/pydata/pandas/issues/3675
250+
.. _GH3682: https://github.com/pydata/pandas/issues/3682
249251

250252
pandas 0.11.0
251253
=============

doc/source/v0.11.1.txt

+21
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,26 @@ Bug Fixes
210210
- Duplicate indexes with and empty DataFrame.from_records will return a correct frame (GH3562_)
211211
- Concat to produce a non-unique columns when duplicates are across dtypes is fixed (GH3602_)
212212

213+
For example you can do
214+
215+
.. ipython :: python
216+
217+
df = DataFrame({'a': list('ab..'), 'b': [1, 2, 3, 4]})
218+
df.replace(regex=r'\s*\.\s*', value=nan)
219+
220+
to replace all occurrences of the string ``'.'`` with zero or more
221+
instances of surrounding whitespace with ``NaN``.
222+
223+
Regular string replacement still works as expected. For example, you can do
224+
225+
.. ipython :: python
226+
227+
df.replace('.', nan)
228+
229+
to replace all occurrences of the string ``'.'`` with ``NaN``.
230+
231+
- ``DataFrame.from_records`` did not accept empty recarrays (GH3682_)
232+
213233
See the `full release notes
214234
<https://github.com/pydata/pandas/blob/master/RELEASE.rst>`__ or issue tracker
215235
on GitHub for a complete list.
@@ -249,3 +269,4 @@ on GitHub for a complete list.
249269
.. _GH3582: https://github.com/pydata/pandas/issues/3582
250270
.. _GH3676: https://github.com/pydata/pandas/issues/3676
251271
.. _GH3675: https://github.com/pydata/pandas/issues/3675
272+
.. _GH3682: https://github.com/pydata/pandas/issues/3682

pandas/core/frame.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -5756,7 +5756,11 @@ def _to_arrays(data, columns, coerce_float=False, dtype=None):
57565756

57575757
return arrays, columns
57585758

5759-
if len(data) == 0:
5759+
if not len(data):
5760+
if isinstance(data, np.ndarray):
5761+
columns = data.dtype.names
5762+
if columns is not None:
5763+
return [[]] * len(columns), columns
57605764
return [], [] # columns if columns is not None else []
57615765
if isinstance(data[0], (list, tuple)):
57625766
return _list_to_arrays(data, columns, coerce_float=coerce_float,

pandas/tests/test_frame.py

+12
Original file line numberDiff line numberDiff line change
@@ -3522,6 +3522,18 @@ def test_from_records_empty(self):
35223522
expected = DataFrame(columns=['a','b','b'])
35233523
assert_frame_equal(result, expected)
35243524

3525+
def test_from_records_empty_with_nonempty_fields_gh3682(self):
3526+
a = np.array([(1, 2)], dtype=[('id', np.int64), ('value', np.int64)])
3527+
df = DataFrame.from_records(a, index='id')
3528+
assert_array_equal(df.index, Index([1], name='id'))
3529+
self.assertEqual(df.index.name, 'id')
3530+
assert_array_equal(df.columns, Index(['value']))
3531+
3532+
b = np.array([], dtype=[('id', np.int64), ('value', np.int64)])
3533+
df = DataFrame.from_records(b, index='id')
3534+
assert_array_equal(df.index, Index([], name='id'))
3535+
self.assertEqual(df.index.name, 'id')
3536+
35253537
def test_to_records_floats(self):
35263538
df = DataFrame(np.random.rand(10, 10))
35273539
df.to_records()

0 commit comments

Comments
 (0)