Skip to content

Commit ee0eaab

Browse files
jbrockmendelMateusz Górski
authored and
Mateusz Górski
committed
remove unnecessary exception (pandas-dev#29538)
1 parent 35edfb1 commit ee0eaab

File tree

1 file changed

+42
-51
lines changed

1 file changed

+42
-51
lines changed

pandas/core/groupby/generic.py

+42-51
Original file line numberDiff line numberDiff line change
@@ -1227,61 +1227,52 @@ def first_not_none(values):
12271227
# GH 8467
12281228
return self._concat_objects(keys, values, not_indexed_same=True)
12291229

1230-
try:
1231-
if self.axis == 0 and isinstance(v, ABCSeries):
1232-
# GH6124 if the list of Series have a consistent name,
1233-
# then propagate that name to the result.
1234-
index = v.index.copy()
1235-
if index.name is None:
1236-
# Only propagate the series name to the result
1237-
# if all series have a consistent name. If the
1238-
# series do not have a consistent name, do
1239-
# nothing.
1240-
names = {v.name for v in values}
1241-
if len(names) == 1:
1242-
index.name = list(names)[0]
1243-
1244-
# normally use vstack as its faster than concat
1245-
# and if we have mi-columns
1246-
if (
1247-
isinstance(v.index, MultiIndex)
1248-
or key_index is None
1249-
or isinstance(key_index, MultiIndex)
1250-
):
1251-
stacked_values = np.vstack([np.asarray(v) for v in values])
1252-
result = DataFrame(
1253-
stacked_values, index=key_index, columns=index
1254-
)
1255-
else:
1256-
# GH5788 instead of stacking; concat gets the
1257-
# dtypes correct
1258-
from pandas.core.reshape.concat import concat
1259-
1260-
result = concat(
1261-
values,
1262-
keys=key_index,
1263-
names=key_index.names,
1264-
axis=self.axis,
1265-
).unstack()
1266-
result.columns = index
1267-
elif isinstance(v, ABCSeries):
1230+
if self.axis == 0 and isinstance(v, ABCSeries):
1231+
# GH6124 if the list of Series have a consistent name,
1232+
# then propagate that name to the result.
1233+
index = v.index.copy()
1234+
if index.name is None:
1235+
# Only propagate the series name to the result
1236+
# if all series have a consistent name. If the
1237+
# series do not have a consistent name, do
1238+
# nothing.
1239+
names = {v.name for v in values}
1240+
if len(names) == 1:
1241+
index.name = list(names)[0]
1242+
1243+
# normally use vstack as its faster than concat
1244+
# and if we have mi-columns
1245+
if (
1246+
isinstance(v.index, MultiIndex)
1247+
or key_index is None
1248+
or isinstance(key_index, MultiIndex)
1249+
):
12681250
stacked_values = np.vstack([np.asarray(v) for v in values])
12691251
result = DataFrame(
1270-
stacked_values.T, index=v.index, columns=key_index
1252+
stacked_values, index=key_index, columns=index
12711253
)
12721254
else:
1273-
# GH#1738: values is list of arrays of unequal lengths
1274-
# fall through to the outer else clause
1275-
# TODO: sure this is right? we used to do this
1276-
# after raising AttributeError above
1277-
return Series(
1278-
values, index=key_index, name=self._selection_name
1279-
)
1280-
1281-
except ValueError:
1282-
# TODO: not reached in tests; is this still needed?
1283-
# GH1738: values is list of arrays of unequal lengths fall
1284-
# through to the outer else clause
1255+
# GH5788 instead of stacking; concat gets the
1256+
# dtypes correct
1257+
from pandas.core.reshape.concat import concat
1258+
1259+
result = concat(
1260+
values,
1261+
keys=key_index,
1262+
names=key_index.names,
1263+
axis=self.axis,
1264+
).unstack()
1265+
result.columns = index
1266+
elif isinstance(v, ABCSeries):
1267+
stacked_values = np.vstack([np.asarray(v) for v in values])
1268+
result = DataFrame(
1269+
stacked_values.T, index=v.index, columns=key_index
1270+
)
1271+
else:
1272+
# GH#1738: values is list of arrays of unequal lengths
1273+
# fall through to the outer else clause
1274+
# TODO: sure this is right? we used to do this
1275+
# after raising AttributeError above
12851276
return Series(values, index=key_index, name=self._selection_name)
12861277

12871278
# if we have date/time like in the original, then coerce dates

0 commit comments

Comments
 (0)