Skip to content

Commit 0ba4d23

Browse files
Joe Jevnikllllllllll
authored andcommitted
BUG: preserve index in _aligned_series
1 parent 05bf71d commit 0ba4d23

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

empyrical/stats.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,32 @@ def excess_sharpe(returns, factor_returns, out=None):
906906
roll_excess_sharpe = _create_binary_vectorized_roll_function(excess_sharpe)
907907

908908

909+
def _to_pandas(ob):
910+
"""Convert an array-like to a pandas object.
911+
912+
Parameters
913+
----------
914+
ob : array-like
915+
The object to convert.
916+
917+
Returns
918+
-------
919+
pandas_structure : pd.Series or pd.DataFrame
920+
The correct structure based on the dimensionality of the data.
921+
"""
922+
if isinstance(ob, (pd.Series, pd.DataFrame)):
923+
return ob
924+
925+
if ob.ndim == 1:
926+
return pd.Series(ob)
927+
elif ob.ndim == 2:
928+
return pd.DataFrame(ob)
929+
else:
930+
raise ValueError(
931+
'cannot convert array of dim > 2 to a pandas structure',
932+
)
933+
934+
909935
def _aligned_series(*many_series):
910936
"""
911937
Return a new list of series containing the data in the input series, but
@@ -934,7 +960,7 @@ def _aligned_series(*many_series):
934960
# dataframe has no ``itervalues``
935961
return (
936962
v
937-
for _, v in iteritems(pd.concat(map(pd.Series, many_series), axis=1))
963+
for _, v in iteritems(pd.concat(map(_to_pandas, many_series), axis=1))
938964
)
939965

940966

0 commit comments

Comments
 (0)