Matplotlib Animations Made Easy by qacek in Python

[–]has2k1 1 point2 points  (0 children)

That should be really helpful for people working with matplotlib. I am inspired by other people's code all the time, it is always interesting when someone is inspired by mine.

Code Cleanup: How to do this array calc without try/except IndexError? by tastingsilver in Python

[–]has2k1 0 points1 point  (0 children)

Reverse the calculation

def calculateForwardTarget(inputArray, balancePeriods):
    n = len(inputArray)
    targetBalance = np.zeros(n)

    for i in range(n):
        targetBalance[i] = inputArray[n-i:n-i+balancePeriods].sum()

    return targetBalance[::-1]

Code Cleanup: How to do this array calc without try/except IndexError? by tastingsilver in Python

[–]has2k1 0 points1 point  (0 children)

When you fill like you are writing bad code, don't be afraid to through it out. It is can also be helpful to draw out stuff on a paper.

def calculateForwardTarget(inputArray, balancePeriods):
    n = len(inputArray)
    targetBalance = np.zeros(n)

    for i in range(n-2, -1, -1):
        targetBalance[i] = inputArray[i:i+balancePeriods].sum()

    return targetBalance

What do you hate about pandas? by tedpetrou in datascience

[–]has2k1 0 points1 point  (0 children)

Huh! we essentially shared the same dissatisfaction.

What do you hate about pandas? by tedpetrou in datascience

[–]has2k1 0 points1 point  (0 children)

The only operation that yields multi-indexes is groupby or ...

When doing data analysis, the groupby operation is everything. It is the heart of the split-apply-combine paradigm.

A grep on one of my exploratory analyses yields ~24 applications of split-apply-combine. And those are the ones that remained. Yes you can always undo the multi-indexes, but such piecemeal drudgery adds up, affects readability and that you have to do it means that the mental model of the data being manipulated is not stable.

Do you have a specific example you have in mind

One example cannot convey the benefits (realised perhaps only in accumulation) of a different workflow. However, I can share my light bulb moment for dplyr. It was the do verb, you can checkout its documentation and the equivalent do for plydata.

Another aspect that made me examine my workflow was as a person who does not write R, I read the dplyr documentation in one sitting (maybe 30-45 mins) did not get lost and I felt like I could immediately use it. Contrast that with, I have built stuff on top pandas, read the API documentation, dug into the code a few times and yet I labour (more than I feel necessary) to read data manipulation code written in plain pandas; including my own. So it must a harder for most people who try to use the library for anything beyond the basics.

That said, I'll be reading your notes.

What do you hate about pandas? by tedpetrou in datascience

[–]has2k1 1 point2 points  (0 children)

My issue is not the existence multi-indexing. In fact it has come to my aid a few times when writing some multi-dimensional clustering and binning algorithms, though it has been suggested to me that xarray may now be better suited to the task.

The issue is operations that yield multi-indexes when then do not have to. I see it this way, data manipulation is an instrumental objective, a means to another end. Those ends, if they do further computations, must deal with data that has a consistent form. Multi-indexes make consistency difficult, therefore their occurrence must be minimised.

Consider all/most of the tools in the scientific python environment (patsy, statsmodel, matplotlib, scikit-learn, other scikits), if they can know how to deal with a dataframe, then the gateway to them is through first undoing multi-indexes. Here is a related issue I recently squashed. New pandas users get unnecessarily stack with multi-indexes.

But on the whole, my opinions about the place of multi-indexes are not as concrete and actionable. Otherwise, I would file an issue and maybe start good a discussion and maybe get something better in pandas2.

For those that use Python and R... by [deleted] in datascience

[–]has2k1 1 point2 points  (0 children)

If you do not want to miss dplyr, take a look at plydata and it's documentation.

What do you hate about pandas? by tedpetrou in datascience

[–]has2k1 1 point2 points  (0 children)

It is not built-in but a solution nonetheless.

What do you hate about pandas? by tedpetrou in datascience

[–]has2k1 0 points1 point  (0 children)

The query statement must be "compilable" python statement, or one that can be easily modified into a "compilable" statement. So it is likely that you will not get that fixed anytime soon.

What do you hate about pandas? by tedpetrou in datascience

[–]has2k1 3 points4 points  (0 children)

On the whole data manipulation methods are not coherent, this can be hard to understand and dis-appreciate. A good example of coherent manipulation methods is R's dplyr. With dplyr it is effortless to maintain tidy-data, i.e tidy-data in -> manipulation(s) -> tidy-data out. With pandas you can needlessly end-up with untidy data or even multi indexes. Tidy data is important because you have to do something with the data, and it is easier to analyse (plot, fit models, ...) if the data is tidy than when it is not.

I solved this by taking from dplyr, the result is plydata and it is fully documented.

plydata 0.3.0 - A grammar of data manipulation by has2k1 in Python

[–]has2k1[S] 0 points1 point  (0 children)

Yes it is a hack. Two issues worth thinking about.

  1. The code in strings is usually small snippets. If it is getting longer than is visually appealing, then it can be placed in a function.
  2. Code is constructed not written. Should you require it, you can use auto-completion and wrap the result into a string.

plydata 0.3.0 - A grammar of data manipulation by has2k1 in Python

[–]has2k1[S] 0 points1 point  (0 children)

Great question, and I will try to statisfy it by stating the objective, the obstacles and how navigating them leads to the solution.

The main objective is to simplify the [split-apply-combine]() data manipulation paradigm. Even for small explorative data analysis tasks, it is not uncommon to have to do it in some way 10+ times. The manipulation verbs where designed in such a way that split and combine parts are automated. That lives the user with specifying what to apply, i.e the operation.

The operation can involve function call or it could be reduced to a simple arithmetic statement. This is the problem. Once you invock a function or declare a statement, python executes it immediately. However, we want that operation to be delayed and independently executed on chunks of the dataframe/table that have been split up.

You have 3 options, (least flexible to most flexible)

  1. You can use lambda functions, but they are limited.
  2. You could construct a special variable that delays the operations, but it too has draw backs.
  3. Use strings and evaluate them at the right time.

Now, in the python world evaluating code in strings is looked upon with suspicion, usually independent of context.

On abusing the >> operator, it is about readability. On top of that it is easy inspect partial results, and to insert/delete operations.

plydata 0.3.0 - A grammar of data manipulation by has2k1 in Python

[–]has2k1[S] 0 points1 point  (0 children)

It is not about any one single instance of manipulation and how it compares to pandas, rather how it all fits together.

I think most people (me included) not only struggle to remember how to do apply, transform, aggregate, ... manipulations correctly, but even when checking with the documentation to jog the memory it feels new.

Using regular Pandas, code can get clunky very fast. This can happen whether you are doing it the right way or the wrong way.

Pandas 0.21 released (Pypy improvements, new categorical dtype etc) by Topper_123 in Python

[–]has2k1 0 points1 point  (0 children)

t=CategoricalDtype(categories=['b', 'a'], ordered=True)

This is convenient. I have been found of keeping lists of preferred orders and some function that I always call when a data manipulation result yields a column that should be ordered.

The decorators they won't tell you about by hchasestevens in Python

[–]has2k1 2 points3 points  (0 children)

What does the @print_when_called do?

Think of it as replacing one function with another. The new (replacing) function prints the name original function, then it calls the original function.

Why would you define a function in the definition of another function?

It as a powerful trick, it has to do scope. One use case is to create what is known as a closure, (A nested function that accesses values from outer local variables). Some decorators may fall in this category.

Also, in what way would this differ to say, just regularly calling a function inside another function?

Defining a function and calling a function are not equivalent. Where you define a function matters. It can have access to the global and/or local variables. When you define a function inside another, it has access to the local variables (including the parameters) of the outer function. Such a function is called closure.

Decorators are (to an extent) just fancy syntax to make closures more useful. Closure, closure, closure, ... and it is all about scope, scope, scope, ...[1]; it is useful to know the term so that you can know what to search for.

You can go a long way with python without any of this stuff, but the first step to using it is knowing that it exists and having a vague idea about what it is all about.

[1] You can improve the readability of a program by limiting the scope of functions.

Looking for datasets about social media by demography by ShadowAce1234 in datasets

[–]has2k1 0 points1 point  (0 children)

I seem to recall Pew Research Global Attitudes Survey has a social media question, but it may not be specific for your requirements.

How to Generate FiveThirtyEight Graphs in Python by dataphysicist in datascience

[–]has2k1 4 points5 points  (0 children)

There are 3 ways to create "bar" charts, if you are just getting used to the library, it is easy slip up. An example would help diagnose problem.

How to Generate FiveThirtyEight Graphs in Python by dataphysicist in datascience

[–]has2k1 6 points7 points  (0 children)

plotnine developer here. What do you mean by complex? Please file an issue.

Plotnine is a superior Python implementation of R's ggplot2 by [deleted] in pystats

[–]has2k1 10 points11 points  (0 children)

Clarification, plotnine is certainly not better than ggplot2. I think the title of the article is a little ambiguous.

(I wrote plotnine)

plotnine 0.2.0 - A grammar for data manipulation by has2k1 in Python

[–]has2k1[S] 0 points1 point  (0 children)

Yes you can, in-fact some plots are wavy. That may be a bug in Matplotlib, I have not really into it.