Author Topic: var  (Read 1847 times)

mtsm

  • Newbie
  • *
  • Posts: 13
  • Karma: +0/-0
    • View Profile
var
« on: May 31, 2012, 02:19:36 AM »
Hi,

two questions on var.

why does var always come across to python as a list of list even when passing a row or column shaped excel range to a pyxll function?

more importantly, I noticed that when I pass a 2d excel range containing mixed types, such as specifically doubles and strings typed as a var to a pyxll function, then I get a list of lists of
strings (not expected) in python. If I pass in a 2d excel range that only contains doubles, I get
a list of lists that contains floats, as expected.

Do you know why that is?
Thanks,
mtsm

mtsm

  • Newbie
  • *
  • Posts: 13
  • Karma: +0/-0
    • View Profile
Re: var
« Reply #1 on: May 31, 2012, 02:51:57 AM »
forget my second question, I was building a numpy array, which converts somehow
to the most complex type, i.e. strings in my example.

Tony

  • Administrator
  • Hero Member
  • *****
  • Posts: 606
  • Karma: +14/-1
    • View Profile
Re: var
« Reply #2 on: June 04, 2012, 01:44:15 PM »
Hi,

ranges in Excel are inherently 2d, even if they are rows or columns.

You can use the numpy_row or numpy_column types for 1d rows and columns, or a custom type to convert the 2d list of lists to a single list (see the custom types example 'flatten').

Best regards,
Tony

mtsm

  • Newbie
  • *
  • Posts: 13
  • Karma: +0/-0
    • View Profile
Re: var
« Reply #3 on: June 04, 2012, 09:02:22 PM »
yeah, I'll look into that. I would imagine that many people just have a line in their code in which they
retrieve the list element 0 whenever they assume that the range supplied would effectively have been
a row or a column of excel cells.

It is  not clear to me why you cannot detect this and then coerce the var into a list instead of a one
element list of lists.

numpy_arrays are not so convenient, since they are typed as far as I know. var is superior as it allows
various types.

Tony

  • Administrator
  • Hero Member
  • *****
  • Posts: 606
  • Karma: +14/-1
    • View Profile
Re: var
« Reply #4 on: June 06, 2012, 07:47:57 PM »
Hi,

imagine you'd written a function that always takes a 2d array. You then pass a single row or column and expect to get something of shape (N,1) or (1,N) but instead you get a 1d array back... you'd then be posting here the exact opposite of what you're asking ;) I think having everything behave consistently is preferable to treating 2d arrays that just happen to be of length 1 in one dimension as a special case.

Best regards,
Tony