Extracting time duration in the session from 30 million rows
A few basic points:
The bool datatype is basically just an int and therefore 1 == True and 0 == False. This makes your two ternaries easier to read, because instead of
purchased=1 if session in buyersession else 0
segmentchange=1 if (segment[maxtime.hour]!=segment[mintime.hour]) else 0 

You can just write
purchased = session in buyersession
segmentchange = segment[maxtime.hour] != segment[mintime.hour]


noofnavigations is a bad name. You should at least call it no_of_navigations, to go along with PEP8. But why not just navigations?
While we're here, you might as well comply with PEP8 on the other things as well (mainly, put spaces around all infix operators and equal signs).

If you demote all of your temporary variables (which you only use once anyways) to keys in a dictionary you can make the printing a lot easier:
with open('/tmp/gensim/yoochooseclick_new.dat', 'w') as f1:
    for session in clickersession:
        temp = clicksdat[clicksdat.Sid == session]
        maxtime = dt.datetime.strptime(temp.Timestamp.max(), &quot;%Y-%m-%dT%H:%M:%S.%fZ&quot;)
        mintime = dt.datetime.strptime(temp.Timestamp.min(), &quot;%Y-%m-%dT%H:%M:%S.%fZ&quot;)
        d = {'purchased': session in buyersession,
             'day': maxtime.day,
             'month': maxtime.month,
             'navigations': len(temp.Timestamp)-1,
             'duration': int((maxtime-mintime).total_seconds()),
             'totalcats': len(temp.itemcategory.unique()),
             'totalitems': len(temp.itemid.unique()),
             'timesegment': segment[maxtime.hour],
             'segmentchange': segment[maxtime.hour] != segment[mintime.hour],
             }
        s = &quot;{purchased}, {day}, {month}, {navigations}, {duration}, {totalcats}, {totalitems}, {timesegment}&quot;
        f1.write(s.format(**d))


Lastly, the building of the words for the different times of day could be written slightly more concise when using a list instead of a dictionary:
segment = ['EM']*24
segment[7:10] = ['M']*3
segment[10:13] = ['A']*3
segment[13:18] = ['E']*5
segment[18:23] = ['N']*5
segment[23] = 'MN'


In the end your code is probably slow, because of all the finding of mins, maxs and lens. This just takes time. Would be good to see a run with a profiler to see at which operation, though.

With four more years of experience, here is how you could make this even faster:
def process(group):
    min_time = group.loc[group[&quot;Timestamp&quot;].idxmin()]
    max_time = group.loc[group[&quot;Timestamp&quot;].idxmax()]
    diff = max_time[&quot;Timestamp&quot;] - min_time[&quot;Timestamp&quot;]
    return pd.Series({&quot;purchased&quot;: group[&quot;Sid&quot;].iloc[0] in buyer_session,
                      &quot;day&quot;: max_time[&quot;Timestamp&quot;].day,
                      &quot;month&quot;: max_time[&quot;Timestamp&quot;].month,
                      &quot;navigations&quot;: group[&quot;Timestamp&quot;].count() - 1,
                      &quot;duration&quot;: int(diff.total_seconds()),
                      &quot;totalcats&quot;: group[&quot;itemcategory&quot;].nunique(),
                      &quot;totalitems&quot;: group[&quot;itemid&quot;].nunique(),
                      &quot;timesegment&quot;: max_time[&quot;segment&quot;],
                      &quot;segmentchange&quot;: max_time[&quot;segment&quot;] != min_time[&quot;segment&quot;]})
    
if __name__ == &quot;__main__&quot;:
    buys_data = pd.read_csv(...)
    buyer_session = set(buys_data[&quot;Sid&quot;].unique())
    clicks_data = pd.read_csv(...)
    clicks_data[&quot;Timestamp&quot;] = pd.to_datetime(clicks_data[&quot;Timestamp&quot;], 
                                              format=&quot;%Y-%m-%dT%H:%M:%S.%fZ&quot;)
    clicks_data[&quot;segment&quot;] = pd.cut(clicks_data[&quot;Timestamp&quot;].dt.hour,
                                    [0, 7, 10, 13, 18, 23, 24],
                                    labels=[&quot;EM&quot;, &quot;M&quot;, &quot;A&quot;, &quot;E&quot;, &quot;N&quot;, &quot;MN&quot;])
    sessions = clicks_data.groupby(&quot;Sid&quot;, as_index=False).apply(process)
    sessions.to_csv('/tmp/gensim/yoochooseclick_new.dat', index=False)

I used the fact that you can directly get the number of unique items using nunique and that it is easier to group by the session ID than iterate over it using groupby (provided you are actually interested in all session IDs). I also converted to datetime objects right away and assigned the segements using cut. Before, there was a possible bug, because you determined the maximum and minimum date using string comparison. For the datetime format you have, this just happens to also sort correctly by date, but if that format ever changes, your code would break.