{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Chapter 8 - How to deal with timestamps"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 8.1 Parsing Unix timestamps"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"popcon = pd.read_csv('./data/popularity-contest', sep = ' ')[: -1]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"popcon.columns = ['atime', 'ctime', 'package_name', 'mru_program', 'tag']"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"
\n",
" \n",
" \n",
" | \n",
" atime | \n",
" ctime | \n",
" package_name | \n",
" mru_program | \n",
" tag | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" 1387295797 | \n",
" 1367633260 | \n",
" perl-base | \n",
" /usr/bin/perl | \n",
" NaN | \n",
"
\n",
" \n",
" | 1 | \n",
" 1387295796 | \n",
" 1354370480 | \n",
" login | \n",
" /bin/su | \n",
" NaN | \n",
"
\n",
" \n",
" | 2 | \n",
" 1387295743 | \n",
" 1354341275 | \n",
" libtalloc2 | \n",
" /usr/lib/x86_64-linux-gnu/libtalloc.so.2.0.7 | \n",
" NaN | \n",
"
\n",
" \n",
" | 3 | \n",
" 1387295743 | \n",
" 1387224204 | \n",
" libwbclient0 | \n",
" /usr/lib/x86_64-linux-gnu/libwbclient.so.0 | \n",
" <RECENT-CTIME> | \n",
"
\n",
" \n",
" | 4 | \n",
" 1387295742 | \n",
" 1354341253 | \n",
" libselinux1 | \n",
" /lib/x86_64-linux-gnu/libselinux.so.1 | \n",
" NaN | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" atime ctime package_name \\\n",
"0 1387295797 1367633260 perl-base \n",
"1 1387295796 1354370480 login \n",
"2 1387295743 1354341275 libtalloc2 \n",
"3 1387295743 1387224204 libwbclient0 \n",
"4 1387295742 1354341253 libselinux1 \n",
"\n",
" mru_program tag \n",
"0 /usr/bin/perl NaN \n",
"1 /bin/su NaN \n",
"2 /usr/lib/x86_64-linux-gnu/libtalloc.so.2.0.7 NaN \n",
"3 /usr/lib/x86_64-linux-gnu/libwbclient.so.0 \n",
"4 /lib/x86_64-linux-gnu/libselinux.so.1 NaN "
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"popcon.head()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"popcon['atime'] = popcon['atime'].astype(int)\n",
"popcon['ctime'] = popcon['ctime'].astype(int)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"popcon['atime'] = pd.to_datetime(popcon['atime'], unit = 's')"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"popcon['ctime'] = pd.to_datetime(popcon['ctime'], unit = 's')"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"dtype('\n",
"\n",
" \n",
" \n",
" | \n",
" atime | \n",
" ctime | \n",
" package_name | \n",
" mru_program | \n",
" tag | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" 2013-12-17 15:56:37 | \n",
" 2013-05-04 02:07:40 | \n",
" perl-base | \n",
" /usr/bin/perl | \n",
" NaN | \n",
"
\n",
" \n",
" | 1 | \n",
" 2013-12-17 15:56:36 | \n",
" 2012-12-01 14:01:20 | \n",
" login | \n",
" /bin/su | \n",
" NaN | \n",
"
\n",
" \n",
" | 2 | \n",
" 2013-12-17 15:55:43 | \n",
" 2012-12-01 05:54:35 | \n",
" libtalloc2 | \n",
" /usr/lib/x86_64-linux-gnu/libtalloc.so.2.0.7 | \n",
" NaN | \n",
"
\n",
" \n",
" | 3 | \n",
" 2013-12-17 15:55:43 | \n",
" 2013-12-16 20:03:24 | \n",
" libwbclient0 | \n",
" /usr/lib/x86_64-linux-gnu/libwbclient.so.0 | \n",
" <RECENT-CTIME> | \n",
"
\n",
" \n",
" | 4 | \n",
" 2013-12-17 15:55:42 | \n",
" 2012-12-01 05:54:13 | \n",
" libselinux1 | \n",
" /lib/x86_64-linux-gnu/libselinux.so.1 | \n",
" NaN | \n",
"
\n",
" \n",
"
\n",
""
],
"text/plain": [
" atime ctime package_name \\\n",
"0 2013-12-17 15:56:37 2013-05-04 02:07:40 perl-base \n",
"1 2013-12-17 15:56:36 2012-12-01 14:01:20 login \n",
"2 2013-12-17 15:55:43 2012-12-01 05:54:35 libtalloc2 \n",
"3 2013-12-17 15:55:43 2013-12-16 20:03:24 libwbclient0 \n",
"4 2013-12-17 15:55:42 2012-12-01 05:54:13 libselinux1 \n",
"\n",
" mru_program tag \n",
"0 /usr/bin/perl NaN \n",
"1 /bin/su NaN \n",
"2 /usr/lib/x86_64-linux-gnu/libtalloc.so.2.0.7 NaN \n",
"3 /usr/lib/x86_64-linux-gnu/libwbclient.so.0 \n",
"4 /lib/x86_64-linux-gnu/libselinux.so.1 NaN "
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"popcon.head()"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"popcon = popcon[popcon['ctime'] > '1970-01-01']"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"nonlibraries = popcon[~popcon['package_name'].str.contains('lib')]"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/wangshuailong/Library/Python/2.7/lib/python/site-packages/ipykernel/__main__.py:1: FutureWarning: sort(columns=....) is deprecated, use sort_values(by=.....)\n",
" if __name__ == '__main__':\n"
]
},
{
"data": {
"text/html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" atime | \n",
" ctime | \n",
" package_name | \n",
" mru_program | \n",
" tag | \n",
"
\n",
" \n",
" \n",
" \n",
" | 57 | \n",
" 2013-12-17 04:55:39 | \n",
" 2013-12-17 04:55:42 | \n",
" ddd | \n",
" /usr/bin/ddd | \n",
" <RECENT-CTIME> | \n",
"
\n",
" \n",
" | 450 | \n",
" 2013-12-16 20:03:20 | \n",
" 2013-12-16 20:05:13 | \n",
" nodejs | \n",
" /usr/bin/npm | \n",
" <RECENT-CTIME> | \n",
"
\n",
" \n",
" | 454 | \n",
" 2013-12-16 20:03:20 | \n",
" 2013-12-16 20:05:04 | \n",
" switchboard-plug-keyboard | \n",
" /usr/lib/plugs/pantheon/keyboard/options.txt | \n",
" <RECENT-CTIME> | \n",
"
\n",
" \n",
" | 445 | \n",
" 2013-12-16 20:03:20 | \n",
" 2013-12-16 20:05:04 | \n",
" thunderbird-locale-en | \n",
" /usr/lib/thunderbird-addons/extensions/langpac... | \n",
" <RECENT-CTIME> | \n",
"
\n",
" \n",
" | 396 | \n",
" 2013-12-16 20:08:27 | \n",
" 2013-12-16 20:05:03 | \n",
" software-center | \n",
" /usr/sbin/update-software-center | \n",
" <RECENT-CTIME> | \n",
"
\n",
" \n",
" | 449 | \n",
" 2013-12-16 20:03:20 | \n",
" 2013-12-16 20:05:00 | \n",
" samba-common-bin | \n",
" /usr/bin/net.samba3 | \n",
" <RECENT-CTIME> | \n",
"
\n",
" \n",
" | 397 | \n",
" 2013-12-16 20:08:25 | \n",
" 2013-12-16 20:04:59 | \n",
" postgresql-client-9.1 | \n",
" /usr/lib/postgresql/9.1/bin/psql | \n",
" <RECENT-CTIME> | \n",
"
\n",
" \n",
" | 398 | \n",
" 2013-12-16 20:08:23 | \n",
" 2013-12-16 20:04:58 | \n",
" postgresql-9.1 | \n",
" /usr/lib/postgresql/9.1/bin/postmaster | \n",
" <RECENT-CTIME> | \n",
"
\n",
" \n",
" | 452 | \n",
" 2013-12-16 20:03:20 | \n",
" 2013-12-16 20:04:55 | \n",
" php5-dev | \n",
" /usr/include/php5/main/snprintf.h | \n",
" <RECENT-CTIME> | \n",
"
\n",
" \n",
" | 440 | \n",
" 2013-12-16 20:03:20 | \n",
" 2013-12-16 20:04:54 | \n",
" php-pear | \n",
" /usr/share/php/XML/Util.php | \n",
" <RECENT-CTIME> | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" atime ctime package_name \\\n",
"57 2013-12-17 04:55:39 2013-12-17 04:55:42 ddd \n",
"450 2013-12-16 20:03:20 2013-12-16 20:05:13 nodejs \n",
"454 2013-12-16 20:03:20 2013-12-16 20:05:04 switchboard-plug-keyboard \n",
"445 2013-12-16 20:03:20 2013-12-16 20:05:04 thunderbird-locale-en \n",
"396 2013-12-16 20:08:27 2013-12-16 20:05:03 software-center \n",
"449 2013-12-16 20:03:20 2013-12-16 20:05:00 samba-common-bin \n",
"397 2013-12-16 20:08:25 2013-12-16 20:04:59 postgresql-client-9.1 \n",
"398 2013-12-16 20:08:23 2013-12-16 20:04:58 postgresql-9.1 \n",
"452 2013-12-16 20:03:20 2013-12-16 20:04:55 php5-dev \n",
"440 2013-12-16 20:03:20 2013-12-16 20:04:54 php-pear \n",
"\n",
" mru_program tag \n",
"57 /usr/bin/ddd \n",
"450 /usr/bin/npm \n",
"454 /usr/lib/plugs/pantheon/keyboard/options.txt \n",
"445 /usr/lib/thunderbird-addons/extensions/langpac... \n",
"396 /usr/sbin/update-software-center \n",
"449 /usr/bin/net.samba3 \n",
"397 /usr/lib/postgresql/9.1/bin/psql \n",
"398 /usr/lib/postgresql/9.1/bin/postmaster \n",
"452 /usr/include/php5/main/snprintf.h \n",
"440 /usr/share/php/XML/Util.php "
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nonlibraries.sort('ctime', ascending=False)[: 10]"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}