Skip to content
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

Remove blank lines-->fix cookbook rendering issue #23144

Closed
wants to merge 1 commit into from

Conversation

thoo
Copy link
Contributor

@thoo thoo commented Oct 14, 2018

Fixes #23128

@codecov
Copy link

codecov bot commented Oct 14, 2018

Codecov Report

Merging #23144 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master   #23144   +/-   ##
=======================================
  Coverage   92.13%   92.13%           
=======================================
  Files         170      170           
  Lines       51073    51073           
=======================================
  Hits        47056    47056           
  Misses       4017     4017
Flag Coverage Δ
#multiple 90.56% <ø> (ø) ⬆️
#single 42.28% <ø> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 85dc171...7e6864d. Read the comment docs.

@jreback
Copy link
Contributor

jreback commented Oct 14, 2018

what are you removing lines? if anything they improve code readability

@thoo
Copy link
Contributor Author

thoo commented Oct 14, 2018

@jreback Yes I was removing lines. Ipython directive has a bug to cut off a function block if there is a blank line. Please let me know if there is another way to do it.

Copy link
Member

@datapythonista datapythonista left a comment

Choose a reason for hiding this comment

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

It'd be nice to find a better way to fix the problem with the ipython directive, but unless someone knows how to do that, I think we can merge this, which is probably better than having the function incomplete (ipython only renders until the first blank line). See: https://pandas-docs.github.io/pandas-docs-travis/cookbook.html#correlation

@jreback
Copy link
Contributor

jreback commented Oct 14, 2018

better way is to use multiple ipython blocks (caveat is that they cannot span a single function definition)
simpler and more descriptive (as u. can comment on code)
having large blocks of code is an anti-pattern

@datapythonista
Copy link
Member

But can we split the code of a function across different ipython blocks?

I'm thinking that may be a better option is to use a python code block instead of an ipython one. That should fix the problem and we still keep the readability of the code.

Closing this PR. @thoo do you want to take care of that? The idea is to use .. code-block:: python instead. And that requires that instead of the code directly without the outputs, we use >>> and the outputs, for example:

>>> def func():
...      print('hello world')
>>> func()
hello world

@thoo
Copy link
Contributor Author

thoo commented Oct 14, 2018

@datapythonista Sure. Let me take a look. Hopefully it is straight forward.

@thoo
Copy link
Contributor Author

thoo commented Oct 14, 2018

@datapythonista Will this work?

>>> def distcorr(x, y):
...     n = len(x)
...     a = np.zeros(shape=(n, n))
...     b = np.zeros(shape=(n, n))

...     for i in range(n):
...         for j in range(i + 1, n):
...             a[i, j] = abs(x[i] - x[j])
...             b[i, j] = abs(y[i] - y[j])
...     a += a.T
...     b += b.T

...     a_bar = np.vstack([np.nanmean(a, axis=0)] * n)
...     b_bar = np.vstack([np.nanmean(b, axis=0)] * n)

...     A = a - a_bar - a_bar.T + np.full(shape=(n, n), fill_value=a_bar.mean())
...     B = b - b_bar - b_bar.T + np.full(shape=(n, n), fill_value=b_bar.mean())

...     cov_ab = np.sqrt(np.nansum(A * B)) / n
...     std_a = np.sqrt(np.sqrt(np.nansum(A**2)) / n)
...     std_b = np.sqrt(np.sqrt(np.nansum(B**2)) / n)

...     return cov_ab / std_a / std_b

>>> df = pd.DataFrame(np.random.normal(size=(100, 3)))

>>> df.corr(method=distcorr)
       0         1         2
0  1.000000  0.209812  0.132786
1  0.209812  1.000000  0.153526
2  0.132786  0.153526  1.000000

@datapythonista
Copy link
Member

I think you need the ... in the blank lines too. Can you check how to run doctests on the Internet, and find a way to run it to make sure please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants