[Tutor] Named function in a single line

Manprit Singh manpritsinghece at gmail.com
Thu Sep 10 23:47:18 EDT 2020


Dear Sir,

This is again a continuation mail .
Consider a CSV File, containing a column of dates in the format . DD- MM-
YYYY. Now i have to import this file as Pandas DataFrame , while making the
datatype of Date column to datetime[64] and would like that dates should
appear in the form YYYY - MM - DD in the imported data frame .

Code is written below , to perform the above mentioned task:

import numpy as np                                  # It is good to import
numpy as well when using pandas

import pandas as pd                                 # Importing pandas

from datetime import datetime as dt         # Importing Datetime

traffic =
pd.read_csv("traffic.csv",
               # Name of CSV File is traffic.csv
                                  parse_dates = ["Date"],
                                   #  Column "Date" contains dates
                                  date_parser = lambda x :
dt.strptime(x,'%d/%m/%Y'))      # Function that formats the dates in
desired format YYYY - MM --DD

First of all i need to know, if i am correct upto this point ? I mean to
say my understanding of using parse_dates & date_parser in pd.read_csv( )
Now coming to the question if i rewrite the above code as below :

import numpy as np                                  # It is good to import
numpy as well when using pandas

import pandas as pd                                 # Importing pandas

from datetime import datetime as dt         # Importing Datetime

def dt_fmt(x): return dt.strptime(x,'%d/%m/%Y')

traffic =
pd.read_csv("traffic.csv",
# Name of CSV File is traffic.csv

parse_dates=["Date"],
#  Column "Date" contains dates

date_parser=dt_fmt)
# Callable to format the dates in desired format.

If you can see, in this code   I have replaced the lambda function , with
another function object . dt_fmt is the name of this function object, which
is defined just above the pd.read_csv( ) in a single line .

First point is I feel this way  is more clear and clean  as compared to
using the lambda function .
Second point is , if I have more than one CSV file to import with all of
them having the date column written in the same way , this dt_fmt function
can be used for every dataframe.
Third point  is that the dt_fmt function is defined in a single line .  Is
it acceptable to write it in this way in a professional code ?

Need your guidance.

Regards
Manprit Singh








On Fri, Sep 11, 2020 at 1:49 AM Alan Gauld via Tutor <tutor at python.org>
wrote:

> On 10/09/2020 18:34, Manprit Singh wrote:
>
> > def multiply(a, b) : return a * b
> >
> > PEP 8 allows us to write such small functions in a single line ?
>
> Others have addressed the PEP8 issue, I'll offer a slightly different
> take on why you might do this.
>
> When we define a function we are, in computer science terms, creating
> a lambda expression. We can do this explicitly in Python (and must do
> so explicitly in some dialects of Lisp) with:
>
> multiply = lambda a,b: a*b
>
> Which is identical to the function definition above and is used in
> the same way:
>
> print(multiply(4,7))  # prints 28
>
> The lambda form simply reflects the CS theory. But most non CS
> folks don't like (or understand) lambdas so the single line
> definition combines the terseness of the lambda style with the
> familiar def syntax.
>
> For multi line definition lambda can't be used since it only
> works (in python) for single expressions. You can't have loops
> or sequences etc. in a Python lambda.
>
> Don't read this as a recommendation for the lambda style, it's not.
> It's just an observation on the philosophy behind short function
> definitions.
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list