Skip to content

DOC: use black to fix code style in doc pandas-dev#36777 #36824

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 4 commits into from
Oct 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions doc/source/development/extending.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ decorate a class, providing the name of attribute to add. The class's
@staticmethod
def _validate(obj):
# verify there is a column latitude and a column longitude
if 'latitude' not in obj.columns or 'longitude' not in obj.columns:
if "latitude" not in obj.columns or "longitude" not in obj.columns:
raise AttributeError("Must have 'latitude' and 'longitude'.")

@property
Expand All @@ -50,8 +50,9 @@ decorate a class, providing the name of attribute to add. The class's

Now users can access your methods using the ``geo`` namespace:

>>> ds = pd.DataFrame({'longitude': np.linspace(0, 10),
... 'latitude': np.linspace(0, 20)})
>>> ds = pd.Dataframe(
... {"longitude": np.linspace(0, 10), "latitude": np.linspace(0, 20)}
... )
>>> ds.geo.center
(5.0, 10.0)
>>> ds.geo.plot()
Expand Down Expand Up @@ -271,6 +272,7 @@ included as a column in a pandas DataFrame):
def __arrow_array__(self, type=None):
# convert the underlying array values to a pyarrow Array
import pyarrow

return pyarrow.array(..., type=type)

The ``ExtensionDtype.__from_arrow__`` method then controls the conversion
Expand Down Expand Up @@ -377,7 +379,7 @@ Below example shows how to define ``SubclassedSeries`` and ``SubclassedDataFrame
>>> type(to_framed)
<class '__main__.SubclassedDataFrame'>

>>> df = SubclassedDataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
>>> df = SubclassedDataFrame({"A": [1, 2, 3], "B": [4, 5, 6], "C": [7, 8, 9]})
>>> df
A B C
0 1 4 7
Expand All @@ -387,7 +389,7 @@ Below example shows how to define ``SubclassedSeries`` and ``SubclassedDataFrame
>>> type(df)
<class '__main__.SubclassedDataFrame'>

>>> sliced1 = df[['A', 'B']]
>>> sliced1 = df[["A", "B"]]
>>> sliced1
A B
0 1 4
Expand All @@ -397,7 +399,7 @@ Below example shows how to define ``SubclassedSeries`` and ``SubclassedDataFrame
>>> type(sliced1)
<class '__main__.SubclassedDataFrame'>

>>> sliced2 = df['A']
>>> sliced2 = df["A"]
>>> sliced2
0 1
1 2
Expand All @@ -422,39 +424,39 @@ Below is an example to define two original properties, "internal_cache" as a tem
class SubclassedDataFrame2(pd.DataFrame):

# temporary properties
_internal_names = pd.DataFrame._internal_names + ['internal_cache']
_internal_names = pd.DataFrame._internal_names + ["internal_cache"]
_internal_names_set = set(_internal_names)

# normal properties
_metadata = ['added_property']
_metadata = ["added_property"]

@property
def _constructor(self):
return SubclassedDataFrame2

.. code-block:: python

>>> df = SubclassedDataFrame2({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
>>> df = SubclassedDataFrame2({"A": [1, 2, 3], "B": [4, 5, 6], "C": [7, 8, 9]})
>>> df
A B C
0 1 4 7
1 2 5 8
2 3 6 9

>>> df.internal_cache = 'cached'
>>> df.added_property = 'property'
>>> df.internal_cache = "cached"
>>> df.added_property = "property"

>>> df.internal_cache
cached
>>> df.added_property
property

# properties defined in _internal_names is reset after manipulation
>>> df[['A', 'B']].internal_cache
>>> df[["A", "B"]].internal_cache
AttributeError: 'SubclassedDataFrame2' object has no attribute 'internal_cache'

# properties defined in _metadata are retained
>>> df[['A', 'B']].added_property
>>> df[["A", "B"]].added_property
property

.. _extending.plotting-backends:
Expand All @@ -468,7 +470,7 @@ one based on Matplotlib. For example:

.. code-block:: python

>>> pd.set_option('plotting.backend', 'backend.module')
>>> pd.set_option("plotting.backend", "backend.module")
>>> pd.Series([1, 2, 3]).plot()

This would be more or less equivalent to:
Expand Down Expand Up @@ -499,4 +501,4 @@ registers the default "matplotlib" backend as follows.


More information on how to implement a third-party plotting backend can be found at
https://github.com/pandas-dev/pandas/blob/master/pandas/plotting/__init__.py#L1.
https://github.com/pandas-dev/pandas/blob/master/pandas/plotting/__init__.py#L1.
35 changes: 15 additions & 20 deletions doc/source/user_guide/duplicates.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ duplicates present. The output can't be determined, and so pandas raises.
.. ipython:: python
:okexcept:

s1 = pd.Series([0, 1, 2], index=['a', 'b', 'b'])
s1.reindex(['a', 'b', 'c'])
s1 = pd.Series([0, 1, 2], index=["a", "b", "b"])
s1.reindex(["a", "b", "c"])

Other methods, like indexing, can give very surprising results. Typically
indexing with a scalar will *reduce dimensionality*. Slicing a ``DataFrame``
Expand All @@ -39,30 +39,30 @@ return a scalar. But with duplicates, this isn't the case.

.. ipython:: python

df1 = pd.DataFrame([[0, 1, 2], [3, 4, 5]], columns=['A', 'A', 'B'])
df1 = pd.DataFrame([[0, 1, 2], [3, 4, 5]], columns=["A", "A", "B"])
df1

We have duplicates in the columns. If we slice ``'B'``, we get back a ``Series``

.. ipython:: python

df1['B'] # a series
df1["B"] # a series

But slicing ``'A'`` returns a ``DataFrame``


.. ipython:: python

df1['A'] # a DataFrame
df1["A"] # a DataFrame

This applies to row labels as well

.. ipython:: python

df2 = pd.DataFrame({"A": [0, 1, 2]}, index=['a', 'a', 'b'])
df2 = pd.DataFrame({"A": [0, 1, 2]}, index=["a", "a", "b"])
df2
df2.loc['b', 'A'] # a scalar
df2.loc['a', 'A'] # a Series
df2.loc["b", "A"] # a scalar
df2.loc["a", "A"] # a Series

Duplicate Label Detection
~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -121,29 +121,24 @@ will be raised.
.. ipython:: python
:okexcept:

pd.Series(
[0, 1, 2],
index=['a', 'b', 'b']
).set_flags(allows_duplicate_labels=False)
pd.Series([0, 1, 2], index=["a", "b", "b"]).set_flags(allows_duplicate_labels=False)

This applies to both row and column labels for a :class:`DataFrame`

.. ipython:: python
:okexcept:

pd.DataFrame(
[[0, 1, 2], [3, 4, 5]], columns=["A", "B", "C"],
).set_flags(allows_duplicate_labels=False)
pd.DataFrame([[0, 1, 2], [3, 4, 5]], columns=["A", "B", "C"],).set_flags(
allows_duplicate_labels=False
)

This attribute can be checked or set with :attr:`~DataFrame.flags.allows_duplicate_labels`,
which indicates whether that object can have duplicate labels.

.. ipython:: python

df = (
pd.DataFrame({"A": [0, 1, 2, 3]},
index=['x', 'y', 'X', 'Y'])
.set_flags(allows_duplicate_labels=False)
df = pd.DataFrame({"A": [0, 1, 2, 3]}, index=["x", "y", "X", "Y"]).set_flags(
allows_duplicate_labels=False
)
df
df.flags.allows_duplicate_labels
Expand Down Expand Up @@ -198,7 +193,7 @@ operations.
.. ipython:: python
:okexcept:

s1 = pd.Series(0, index=['a', 'b']).set_flags(allows_duplicate_labels=False)
s1 = pd.Series(0, index=["a", "b"]).set_flags(allows_duplicate_labels=False)
s1
s1.head().rename({"a": "b"})

Expand Down
28 changes: 17 additions & 11 deletions doc/source/user_guide/gotchas.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,19 @@ when calling :meth:`~DataFrame.info`:

.. ipython:: python

dtypes = ['int64', 'float64', 'datetime64[ns]', 'timedelta64[ns]',
'complex128', 'object', 'bool']
dtypes = [
"int64",
"float64",
"datetime64[ns]",
"timedelta64[ns]",
"complex128",
"object",
"bool",
]
n = 5000
data = {t: np.random.randint(100, size=n).astype(t) for t in dtypes}
df = pd.DataFrame(data)
df['categorical'] = df['object'].astype('category')
df["categorical"] = df["object"].astype("category")

df.info()

Expand All @@ -40,7 +47,7 @@ as it can be expensive to do this deeper introspection.

.. ipython:: python

df.info(memory_usage='deep')
df.info(memory_usage="deep")

By default the display option is set to ``True`` but can be explicitly
overridden by passing the ``memory_usage`` argument when invoking ``df.info()``.
Expand Down Expand Up @@ -155,7 +162,7 @@ index, not membership among the values.

.. ipython:: python

s = pd.Series(range(5), index=list('abcde'))
s = pd.Series(range(5), index=list("abcde"))
2 in s
'b' in s

Expand Down Expand Up @@ -206,11 +213,11 @@ arrays. For example:

.. ipython:: python

s = pd.Series([1, 2, 3, 4, 5], index=list('abcde'))
s = pd.Series([1, 2, 3, 4, 5], index=list("abcde"))
s
s.dtype

s2 = s.reindex(['a', 'b', 'c', 'f', 'u'])
s2 = s.reindex(["a", "b", "c", "f", "u"])
s2
s2.dtype

Expand All @@ -227,12 +234,11 @@ the nullable-integer extension dtypes provided by pandas

.. ipython:: python

s_int = pd.Series([1, 2, 3, 4, 5], index=list('abcde'),
dtype=pd.Int64Dtype())
s_int = pd.Series([1, 2, 3, 4, 5], index=list("abcde"), dtype=pd.Int64Dtype())
s_int
s_int.dtype

s2_int = s_int.reindex(['a', 'b', 'c', 'f', 'u'])
s2_int = s_int.reindex(["a", "b", "c", "f", "u"])
s2_int
s2_int.dtype

Expand Down Expand Up @@ -334,7 +340,7 @@ constructors using something similar to the following:

.. ipython:: python

x = np.array(list(range(10)), '>i4') # big endian
x = np.array(list(range(10)), ">i4") # big endian
newx = x.byteswap().newbyteorder() # force native byteorder
s = pd.Series(newx)

Expand Down
Loading