-2

This:

cur.execute("SELECT site_number, site_name, latitude FROM mydb.station ORDER BY site_number")

rows = pd.DataFrame(cur.fetchall(), columns=['site_number', 'site_name', 'latitude'])
for item in rows.columns:
    print(item, type(item))

Produces:

site_number <class 'str'>
site_name <class 'str'>
latitude <class 'str'>

But by the time it gets to:

length_hours = day_length(day, v.latitude)

def day_length(day_of_year, latitude):
    ic(type(day_of_year), day_of_year, type(latitude), latitude)

It ends up as:

ic| type(day_of_year): <class 'int'>
    day_of_year: 1
    type(latitude): <class 'decimal.Decimal'>
    latitude: Decimal('-14.3')

What am I missing? Does type(item) not return the actual data type? Is there a conversion happening at some later point?

I'm just trying to understand what's going on.

To illustrate the error, which is what I'm trying to fix...

When it gets to:

p = math.asin(0.39795 * math.cos(0.2163108 + 2 * math.atan(0.9671396 * math.tan(.00860 * (day_of_year - 186)))))
ic(p)
pi = math.pi
ic(type(pi), pi)
day_light_hours = 24 - (24 / pi) * math.acos(
    (math.sin(0.8333 * pi / 180) + math.sin(latitude * pi / 180) * math.sin(p)) / (
            math.cos(latitude * pi / 180) * math.cos(p)))

I get this:

ic| p: -0.40270065067443084
ic| type(pi): <class 'float'>, pi: 3.141592653589793
unsupported operand type(s) for *: 'decimal.Decimal' and 'float'

P.S. I did not write the math in the day_length function, so aren't in a position to change anything in there.

Sean S
  • 1
  • 2
  • 2
    `for item in rows.columns` simply loops over the *column names*, which are strings, not the values…!? – deceze Jun 23 '21 at 05:02

1 Answers1

0

Ahhh. I now realise I'd thought I'd found a solution "how to identify column datatypes" when it was actually not that.

I should have been looking for dtype.

pandas how to check dtype for all columns in a dataframe?

10 Rep
  • 2,217
  • 7
  • 19
  • 33
Sean S
  • 1
  • 2