0

Say I have a function that parses a couple of columns (in the case of a DataFrame) or a couple of scalars into a datetime64.

How can I write one piece of code that works on both scalars and arrays/series? I seem to remember there's a standard way of doing this but can't find it.

As a further part of this, how would I write this one callable if the operations I needed to perform were different between arrays and scalars?

Chris Withers
  • 10,837
  • 4
  • 33
  • 51
  • Well...it depends. It would be easier to look at specific examples of operations you want to perform. Maybe you could add a few lines of code to your question to demonstrate (ideally as a [MCVE](http://stackoverflow.com/help/mcve) like [this](http://stackoverflow.com/q/22072943/553404) or [this](http://stackoverflow.com/q/29629821/553404))? A lot of NumPy code will work just fine with both scalars and arrays. – YXD Jun 11 '15 at 16:11
  • See http://docs.scipy.org/doc/numpy/reference/routines.functional.html, but if you're doing two different things, why aren't you calling two different functions? – leewz Jun 13 '15 at 20:14
  • They're logically the same thing, but the implementations differ as the functions available on a Series, such as series.astype(str) versus str(scalar). – Chris Withers Jun 15 '15 at 09:03

0 Answers0