Skip to content

DOC: update str.cat example #23723

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jan 4, 2019
19 changes: 12 additions & 7 deletions doc/source/text.rst
Original file line number Diff line number Diff line change
Expand Up @@ -306,21 +306,26 @@ The same alignment can be used when ``others`` is a ``DataFrame``:
Concatenating a Series and many objects into a Series
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

All one-dimensional list-likes can be combined in a list-like container (including iterators, ``dict``-views, etc.):
Several items can be combined a list-like container (including iterators, ``dict``-views, etc.), which may contain ``Series``, ``Index`` and ``np.ndarray``.
Note that ``Index`` will align as well, so we change the indexes of ``s`` and ``u`` to strings for the purpose of this example:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure what this last statement about Index actually means. can you reword.


.. ipython:: python

s
u
s.str.cat([u.values,
u.index.astype(str).values], na_rep='-')
s_values = np.array(['a', 'b', 'c', 'd'], dtype=object) # same as s.values
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd remove the comments here. Rather harmless with the example provided but I don't know if that comment will hold universally with all types (thinking EAs in particular) so don't want to give users an impression of that without context

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I though this was clearly referencing the actual series s (as I wanted to motivate the variable name), rather than make a general statement about the relationship between np.array and .values.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i agree, the comment is just confusing

s2 = pd.Series(s_values, index=s_values)
s2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add this in multiple blocks as its too much to complicated here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this whole example needs to be simpler. maybe just leave the index as integers to avoid confusion, IOW focus less on the join in str.cat and more on the list-lke things that are going on.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing this example tries to show is that it works for Series, ndarray, and Index, and Index can only be aligned with a non-integer index. This whole example would be so so much easier with #22225. I've opened an issue for the DF discussion a while ago, and asked you to comment #24046.

u_values = np.array(['b', 'd', 'a', 'c'], dtype=object) # same as u.values
u2 = pd.Series(u_values, index=u_values)
u2
s2.str.cat([pd.Index(['d', 'c', 'b', 'a']), u2, u_values], na_rep='-', join='left')

All elements must match in length to the calling ``Series`` (or ``Index``), except those having an index if ``join`` is not None:
All ``np.ndarrays`` within the passed list-like must match in length to the calling ``Series`` (or ``Index``),
but ``Series`` and ``Index`` may have arbitrary length (as long as alignment is not disabled with ``join is not None``):

.. ipython:: python

v
s.str.cat([u, v], join='outer', na_rep='-')
s.str.cat([v, u, u_values], join='outer', na_rep='-')

If using ``join='right'`` on a list of ``others`` that contains different indexes,
the union of these indexes will be used as the basis for the final concatenation:
Expand Down