Home > Software design >  Computing Arctan2 with 1 argument in Pandas DataFrame
Computing Arctan2 with 1 argument in Pandas DataFrame

Time:11-03

I have a DF whereby I need to compute the arctan of two north/south wind components. However, it seems that the arctan2 function only takes 2 arguments x,y such according to the documentation:

numpy.arctan2(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'arctan2'>

However, I need to divide the x, y components to obtain my desired answer. So, I need to do this -

dfout = np.arctan2(x/y) using 1 argument but the documentation says that I need 2 arguments x,y.

I see the "/" symbol as an option but I'm not sure if that applies or how to do that. Any ideas?

My data looks like this in df:

day hour    Cns       Cew
1   0       126.002   -100.812
1   1      -42.3775    18.6631
1   2       64.3313   -121.167

I need to do this in the example above:

dfout = np.arctan2(df.Cew/df.Cns)

but I get this error -

TypeError: arctan2() takes from 2 to 3 positional arguments but 1 were given

I have tried this but I get a syntax error.

dfout = np.arctan2(df.Cew,df.Cns,/)

Using this below does not provide the correct answer as I am trying to compute the average wind direction using the Cew (EAST-WEST) and Cns (NORTH-SOUTH) components of the wind.

This will NOT work -

dfout = np.arctan2(Cew, Cns).

And, my angles range from -180 to 180 wind direction angles in degrees.

thanks much,

CodePudding user response:

The function that takes 1 argument is called np.arctan. But I am not sure you really need it.

np.arctan(-100.812/126.002)
# -0.6747912684731013
np.arctan2(-100.812, 126.002)
# -0.6747912684731013

So, you see... why would you want to do the division yourself, rather than letting np.arctan2 do it?

Plus, np.arctan2 is better. Because np.arctan can't know the exact angle, since one information is missing. That information being the direction.

Think of a 45° angle. np.arctan(1/1) = π/4. As expected. And, obviously, so is np.arctan2(1,1). But, what if coordinates are (-1,-1)? np.arctan(-1/-1) is also π/4 (obvioulsly: -1/-1=1/1=1). But (-1,-1) angle is not at 45°, but the opposite direction, that is 5π/4 (or -3π/4).

np.arctan(1/1)
# 0.7853981633974483
np.arctan2(1,1)
# 0.7853981633974483
np.arctan(-1/-1)
# 0.7853981633974483
np.arctan2(-1,-1)
# -2.356194490192345

Usually, people want the angle computed from (-1,-1) to be -3π/4, not π/4.

You have such a case in your sample data

np.arctan(18.6631/-42.3775)
# -0.41484291425043723
np.arctan2(18.6631,-42.3775)
# 2.726749739339356

The second one (unless I am missing something from a specificity of your application) is better. You expect (-42.3775, 18.6631) to be somewhere in the second quadrant, that is between π/2 and π. As arctan2 says. But arctan place it in the fourth.

So in your case

# Either
dfout = np.arctan2(df.Cew, df.Cns)
# Or
dfout = np.arctan(df.Cew/df.Cns)

But the first one is probably what you want.

Last remark: arguments of arctan2 are not x and y, but y and x. Likewise, what you should call arctan with is not x/y but y/x. Since I don't know what your Cew and Cns exactly are, I let it as is. But I surmise it means "east-west", and "north-south", so you may want to replace by

dfout = np.arctan2(df.Cns, df.Cew)
# Or
dfout = np.arctan(df.Cns/df.Cew)

But that is not a coding problem, more a math problem (what do you want to compute arctan of), so I let it up to you.

CodePudding user response:

You should probably just use:

dfout = np.arctan2(df.Cew, df.Cns)

(or swapping the inputs np.arctan2(df.Cns, df.Cew) -- it is not clear from the question itself, but you should be able to get around that from the documentation and what you know about your input).


The np.arctan2() implements atan2(y, x), which is meant to solve the ambiguity resulting from the sign combination in the y / x parameter of arctan(y / x), thus requiring the two arguments.

If you do not need to resolve that ambiguity, you can safely use np.arctan(), which only uses one argument -- y / x.

  • Related