{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd \n", "import datetime as dt" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.date(2016, 4, 12)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dt.date(2016, 4, 12)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "someday = dt.date(2010, 1, 20)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'2010-01-20'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "someday.isoformat()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'2010-01-10 08:13:57'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str(dt.datetime(2010, 1, 10, 8, 13, 57))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### O Objeto pandas Timestamp" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Timestamp('2015-03-31 00:00:00')" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.Timestamp('2015-03-31')" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Timestamp('2015-03-31 00:00:00')" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.Timestamp('2015/03/31')" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Timestamp('2013-11-25 00:00:00')" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.Timestamp('2013, 11, 25')" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Timestamp('2015-01-01 00:00:00')" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.Timestamp('1/1/2015')" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Timestamp('2021-03-08 08:35:15')" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.Timestamp('2021-03-08 08:35:15')" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Timestamp('2021-03-08 18:13:29')" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.Timestamp('2021-03-08 6:13:29 PM')" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Timestamp('2015-01-01 00:00:00')" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.Timestamp(dt.date(2015, 1, 1))" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Timestamp('2000-02-03 21:35:22')" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.Timestamp(dt.datetime(2000, 2, 3, 21, 35, 22))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### O Objeto pandas DateTimeIndex" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "DatetimeIndex(['2016-01-02', '2016-12-01', '2009-09-07'], dtype='datetime64[ns]', freq=None)" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dates = ['2016/01/02', '2016,04,12', '2009-09-07']\n", "pd.DatetimeIndex(dates)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "dates = [dt.date(2016, 1, 10), dt.date(1994, 6, 13), dt.date(2003, 12, 29)]\n", "dt_index = pd.DatetimeIndex(dates)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2016-01-10 100\n", "1994-06-13 200\n", "2003-12-29 300\n", "dtype: int64" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "values = [100, 200, 300]\n", "pd.Series(data = values, index=dt_index)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### O Método to_datetime()" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Timestamp('2001-04-19 00:00:00')" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.to_datetime('2001-04-19')" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Timestamp('2015-01-01 00:00:00')" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.to_datetime(dt.date(2015, 1, 1))" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Timestamp('2015-01-01 14:35:20')" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.to_datetime(dt.datetime(2015, 1, 1, 14, 35, 20))" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "DatetimeIndex(['2015-01-01', '2014-02-08', '2016-01-01', '1996-07-04'], dtype='datetime64[ns]', freq=None)" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.to_datetime(['2015-01-01', '2014/02/08', '2016', 'July 4th, 1996'])" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 2015-01-01\n", "1 2014/02/08\n", "2 2016\n", "3 July 4th, 1996\n", "dtype: object" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "times = pd.Series(['2015-01-01', '2014/02/08', '2016', 'July 4th, 1996'])\n", "times" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 2015-01-01\n", "1 2014-02-08\n", "2 2016-01-01\n", "3 1996-07-04\n", "dtype: datetime64[ns]" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.to_datetime(times)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "dates = pd.Series(['July 4th, 1996', '10/04/1991', 'Hello', '2015-02-31'])" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 1996-07-04\n", "1 1991-10-04\n", "2 NaT\n", "3 NaT\n", "dtype: datetime64[ns]" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.to_datetime(dates, errors='coerce')" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "DatetimeIndex(['2012-10-08 18:15:05', '2012-10-09 18:15:05',\n", " '2012-10-10 18:15:05', '2012-10-11 18:15:05',\n", " '2012-10-12 18:15:05'],\n", " dtype='datetime64[ns]', freq=None)" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.to_datetime([1349720105, 1349806505, 1349892905, 1349979305, 1350065705], unit='s')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### O Método date_range()" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "DatetimeIndex(['2016-01-01', '2016-01-02', '2016-01-03', '2016-01-04',\n", " '2016-01-05', '2016-01-06', '2016-01-07', '2016-01-08',\n", " '2016-01-09', '2016-01-10'],\n", " dtype='datetime64[ns]', freq='D')" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.date_range(start='2016-01-01', end='2016-01-10', freq='D')" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "DatetimeIndex(['2016-01-01', '2016-01-04', '2016-01-05', '2016-01-06',\n", " '2016-01-07', '2016-01-08'],\n", " dtype='datetime64[ns]', freq='B')" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.date_range(start='2016-01-01', end='2016-01-10', freq='B')" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "DatetimeIndex(['2016-01-03', '2016-01-10'], dtype='datetime64[ns]', freq='W-SUN')" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.date_range(start='2016-01-01', end='2016-01-10', freq='W')" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "DatetimeIndex(['2016-01-01 00:00:00', '2016-01-01 01:00:00',\n", " '2016-01-01 02:00:00', '2016-01-01 03:00:00',\n", " '2016-01-01 04:00:00', '2016-01-01 05:00:00',\n", " '2016-01-01 06:00:00', '2016-01-01 07:00:00',\n", " '2016-01-01 08:00:00', '2016-01-01 09:00:00',\n", " ...\n", " '2016-01-09 15:00:00', '2016-01-09 16:00:00',\n", " '2016-01-09 17:00:00', '2016-01-09 18:00:00',\n", " '2016-01-09 19:00:00', '2016-01-09 20:00:00',\n", " '2016-01-09 21:00:00', '2016-01-09 22:00:00',\n", " '2016-01-09 23:00:00', '2016-01-10 00:00:00'],\n", " dtype='datetime64[ns]', length=217, freq='H')" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.date_range(start='2016-01-01', end='2016-01-10', freq='H')" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "DatetimeIndex(['2016-01-31', '2016-02-29', '2016-03-31', '2016-04-30',\n", " '2016-05-31', '2016-06-30', '2016-07-31', '2016-08-31',\n", " '2016-09-30', '2016-10-31', '2016-11-30', '2016-12-31'],\n", " dtype='datetime64[ns]', freq='M')" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.date_range(start='2016-01-01', end='2016-12-31', freq='M')" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "DatetimeIndex(['2012-09-09', '2012-09-10', '2012-09-11', '2012-09-12',\n", " '2012-09-13', '2012-09-14', '2012-09-15', '2012-09-16',\n", " '2012-09-17', '2012-09-18', '2012-09-19', '2012-09-20',\n", " '2012-09-21', '2012-09-22', '2012-09-23', '2012-09-24',\n", " '2012-09-25', '2012-09-26', '2012-09-27', '2012-09-28',\n", " '2012-09-29', '2012-09-30', '2012-10-01', '2012-10-02',\n", " '2012-10-03'],\n", " dtype='datetime64[ns]', freq='D')" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.date_range(start='2012-09-09', periods=25, freq='D')" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "DatetimeIndex(['2012-09-10', '2012-09-11', '2012-09-12', '2012-09-13',\n", " '2012-09-14', '2012-09-17', '2012-09-18', '2012-09-19',\n", " '2012-09-20', '2012-09-21', '2012-09-24', '2012-09-25',\n", " '2012-09-26', '2012-09-27', '2012-09-28', '2012-10-01',\n", " '2012-10-02', '2012-10-03', '2012-10-04', '2012-10-05',\n", " '2012-10-08', '2012-10-09', '2012-10-10', '2012-10-11',\n", " '2012-10-12', '2012-10-15', '2012-10-16', '2012-10-17',\n", " '2012-10-18', '2012-10-19', '2012-10-22', '2012-10-23',\n", " '2012-10-24', '2012-10-25', '2012-10-26', '2012-10-29',\n", " '2012-10-30', '2012-10-31', '2012-11-01', '2012-11-02',\n", " '2012-11-05', '2012-11-06', '2012-11-07', '2012-11-08',\n", " '2012-11-09', '2012-11-12', '2012-11-13', '2012-11-14',\n", " '2012-11-15', '2012-11-16'],\n", " dtype='datetime64[ns]', freq='B')" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.date_range(start='2012-09-09', periods=50, freq='B') # B -> Business Days" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "DatetimeIndex(['2012-10-01', '2012-11-01', '2012-12-01', '2013-01-01',\n", " '2013-02-01', '2013-03-01', '2013-04-01', '2013-05-01',\n", " '2013-06-01', '2013-07-01', '2013-08-01', '2013-09-01',\n", " '2013-10-01', '2013-11-01', '2013-12-01', '2014-01-01',\n", " '2014-02-01', '2014-03-01', '2014-04-01', '2014-05-01',\n", " '2014-06-01', '2014-07-01', '2014-08-01', '2014-09-01',\n", " '2014-10-01', '2014-11-01', '2014-12-01', '2015-01-01',\n", " '2015-02-01', '2015-03-01', '2015-04-01', '2015-05-01',\n", " '2015-06-01', '2015-07-01', '2015-08-01', '2015-09-01',\n", " '2015-10-01', '2015-11-01', '2015-12-01', '2016-01-01',\n", " '2016-02-01', '2016-03-01', '2016-04-01', '2016-05-01',\n", " '2016-06-01', '2016-07-01', '2016-08-01', '2016-09-01',\n", " '2016-10-01', '2016-11-01', '2016-12-01', '2017-01-01',\n", " '2017-02-01', '2017-03-01', '2017-04-01', '2017-05-01',\n", " '2017-06-01', '2017-07-01', '2017-08-01', '2017-09-01'],\n", " dtype='datetime64[ns]', freq='MS')" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.date_range(start='2012-09-09', periods=60, freq='MS')" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "DatetimeIndex(['2012-09-09 00:00:00', '2012-09-09 06:00:00',\n", " '2012-09-09 12:00:00', '2012-09-09 18:00:00',\n", " '2012-09-10 00:00:00', '2012-09-10 06:00:00',\n", " '2012-09-10 12:00:00', '2012-09-10 18:00:00',\n", " '2012-09-11 00:00:00', '2012-09-11 06:00:00',\n", " '2012-09-11 12:00:00', '2012-09-11 18:00:00',\n", " '2012-09-12 00:00:00', '2012-09-12 06:00:00',\n", " '2012-09-12 12:00:00', '2012-09-12 18:00:00',\n", " '2012-09-13 00:00:00', '2012-09-13 06:00:00',\n", " '2012-09-13 12:00:00', '2012-09-13 18:00:00',\n", " '2012-09-14 00:00:00', '2012-09-14 06:00:00',\n", " '2012-09-14 12:00:00', '2012-09-14 18:00:00',\n", " '2012-09-15 00:00:00', '2012-09-15 06:00:00',\n", " '2012-09-15 12:00:00', '2012-09-15 18:00:00',\n", " '2012-09-16 00:00:00', '2012-09-16 06:00:00'],\n", " dtype='datetime64[ns]', freq='6H')" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.date_range(start='2012-09-09', periods=30, freq='6H')" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "DatetimeIndex(['1999-12-12', '1999-12-13', '1999-12-14', '1999-12-15',\n", " '1999-12-16', '1999-12-17', '1999-12-18', '1999-12-19',\n", " '1999-12-20', '1999-12-21', '1999-12-22', '1999-12-23',\n", " '1999-12-24', '1999-12-25', '1999-12-26', '1999-12-27',\n", " '1999-12-28', '1999-12-29', '1999-12-30', '1999-12-31'],\n", " dtype='datetime64[ns]', freq='D')" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.date_range(end='1999-12-31', periods=20, freq='D')" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "DatetimeIndex(['1995-08-01', '1995-09-01', '1995-10-01', '1995-11-01',\n", " '1995-12-01', '1996-01-01', '1996-02-01', '1996-03-01',\n", " '1996-04-01', '1996-05-01', '1996-06-01', '1996-07-01',\n", " '1996-08-01', '1996-09-01', '1996-10-01', '1996-11-01',\n", " '1996-12-01', '1997-01-01', '1997-02-01', '1997-03-01',\n", " '1997-04-01', '1997-05-01', '1997-06-01', '1997-07-01',\n", " '1997-08-01', '1997-09-01', '1997-10-01', '1997-11-01',\n", " '1997-12-01', '1998-01-01', '1998-02-01', '1998-03-01',\n", " '1998-04-01', '1998-05-01', '1998-06-01', '1998-07-01',\n", " '1998-08-01', '1998-09-01', '1998-10-01', '1998-11-01',\n", " '1998-12-01', '1999-01-01', '1999-02-01', '1999-03-01',\n", " '1999-04-01', '1999-05-01', '1999-06-01', '1999-07-01',\n", " '1999-08-01', '1999-09-01', '1999-10-01', '1999-11-01',\n", " '1999-12-01'],\n", " dtype='datetime64[ns]', freq='MS')" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.date_range(end='1999-12-31', periods=53, freq='MS')" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "DatetimeIndex(['1999-12-07 23:00:00', '1999-12-08 06:00:00',\n", " '1999-12-08 13:00:00', '1999-12-08 20:00:00',\n", " '1999-12-09 03:00:00', '1999-12-09 10:00:00',\n", " '1999-12-09 17:00:00', '1999-12-10 00:00:00',\n", " '1999-12-10 07:00:00', '1999-12-10 14:00:00',\n", " '1999-12-10 21:00:00', '1999-12-11 04:00:00',\n", " '1999-12-11 11:00:00', '1999-12-11 18:00:00',\n", " '1999-12-12 01:00:00', '1999-12-12 08:00:00',\n", " '1999-12-12 15:00:00', '1999-12-12 22:00:00',\n", " '1999-12-13 05:00:00', '1999-12-13 12:00:00',\n", " '1999-12-13 19:00:00', '1999-12-14 02:00:00',\n", " '1999-12-14 09:00:00', '1999-12-14 16:00:00',\n", " '1999-12-14 23:00:00', '1999-12-15 06:00:00',\n", " '1999-12-15 13:00:00', '1999-12-15 20:00:00',\n", " '1999-12-16 03:00:00', '1999-12-16 10:00:00',\n", " '1999-12-16 17:00:00', '1999-12-17 00:00:00',\n", " '1999-12-17 07:00:00', '1999-12-17 14:00:00',\n", " '1999-12-17 21:00:00', '1999-12-18 04:00:00',\n", " '1999-12-18 11:00:00', '1999-12-18 18:00:00',\n", " '1999-12-19 01:00:00', '1999-12-19 08:00:00',\n", " '1999-12-19 15:00:00', '1999-12-19 22:00:00',\n", " '1999-12-20 05:00:00', '1999-12-20 12:00:00',\n", " '1999-12-20 19:00:00', '1999-12-21 02:00:00',\n", " '1999-12-21 09:00:00', '1999-12-21 16:00:00',\n", " '1999-12-21 23:00:00', '1999-12-22 06:00:00',\n", " '1999-12-22 13:00:00', '1999-12-22 20:00:00',\n", " '1999-12-23 03:00:00', '1999-12-23 10:00:00',\n", " '1999-12-23 17:00:00', '1999-12-24 00:00:00',\n", " '1999-12-24 07:00:00', '1999-12-24 14:00:00',\n", " '1999-12-24 21:00:00', '1999-12-25 04:00:00',\n", " '1999-12-25 11:00:00', '1999-12-25 18:00:00',\n", " '1999-12-26 01:00:00', '1999-12-26 08:00:00',\n", " '1999-12-26 15:00:00', '1999-12-26 22:00:00',\n", " '1999-12-27 05:00:00', '1999-12-27 12:00:00',\n", " '1999-12-27 19:00:00', '1999-12-28 02:00:00',\n", " '1999-12-28 09:00:00', '1999-12-28 16:00:00',\n", " '1999-12-28 23:00:00', '1999-12-29 06:00:00',\n", " '1999-12-29 13:00:00', '1999-12-29 20:00:00',\n", " '1999-12-30 03:00:00', '1999-12-30 10:00:00',\n", " '1999-12-30 17:00:00', '1999-12-31 00:00:00'],\n", " dtype='datetime64[ns]', freq='7H')" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.date_range(end='1999-12-31', periods=80, freq='7H')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### O Acessor .dt" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [], "source": [ "bunch_of_dates = pd.date_range(start='2000-01-01', end='2010-12-31', freq='24D')" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 2000-01-01\n", "1 2000-01-25\n", "2 2000-02-18\n", "3 2000-03-13\n", "4 2000-04-06\n", "dtype: datetime64[ns]" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = pd.Series(bunch_of_dates)\n", "s.head()" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 1\n", "1 25\n", "2 18\n", "3 13\n", "4 6\n", "dtype: int64" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.dt.day.head()" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 Saturday\n", "1 Tuesday\n", "2 Friday\n", "3 Monday\n", "4 Thursday\n", "dtype: object" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.dt.weekday_name.head()" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 2000-01-01\n", "19 2001-04-01\n", "38 2002-07-01\n", "137 2009-01-01\n", "dtype: datetime64[ns]" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mask = s.dt.is_quarter_start \n", "s[mask]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### O Objeto Timedelta" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [], "source": [ "timeA = pd.Timestamp('2016-03-31 04:35:15 PM')\n", "timeB = pd.Timestamp('2016-03-20 02:16:49 AM')" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Timedelta('11 days 14:18:26')" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "timeA - timeB" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Timedelta('59 days 12:45:00')" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.Timedelta(days=3, minutes=45, hours=12, weeks=8)" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Timedelta('14 days 05:12:49')" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.Timedelta('14 days 5 hours 12 minutes 49 seconds')" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
order_datedelivery_date
ID
11998-05-241999-02-05
21992-04-221998-03-06
41991-02-101992-08-26
51992-07-211997-11-20
71993-09-021998-06-10
\n", "
" ], "text/plain": [ " order_date delivery_date\n", "ID \n", "1 1998-05-24 1999-02-05\n", "2 1992-04-22 1998-03-06\n", "4 1991-02-10 1992-08-26\n", "5 1992-07-21 1997-11-20\n", "7 1993-09-02 1998-06-10" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "shipping = pd.read_csv('https://dadosdatascience.netlify.com/ecommerce.csv', index_col='ID', parse_dates=['order_date','delivery_date'])\n", "shipping.head()" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [], "source": [ "shipping['Delivery Time'] = shipping['delivery_date'] - shipping['order_date']" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
order_datedelivery_dateDelivery Time
ID
11998-05-241999-02-05257 days
21992-04-221998-03-062144 days
41991-02-101992-08-26563 days
51992-07-211997-11-201948 days
71993-09-021998-06-101742 days
\n", "
" ], "text/plain": [ " order_date delivery_date Delivery Time\n", "ID \n", "1 1998-05-24 1999-02-05 257 days\n", "2 1992-04-22 1998-03-06 2144 days\n", "4 1991-02-10 1992-08-26 563 days\n", "5 1992-07-21 1997-11-20 1948 days\n", "7 1993-09-02 1998-06-10 1742 days" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "shipping.head()" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [], "source": [ "shipping['Dobro do Tempo'] = shipping['delivery_date'] + shipping['Delivery Time']" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
order_datedelivery_dateDelivery TimeDobro do Tempo
ID
11998-05-241999-02-05257 days1999-10-20
21992-04-221998-03-062144 days2004-01-18
41991-02-101992-08-26563 days1994-03-12
51992-07-211997-11-201948 days2003-03-22
71993-09-021998-06-101742 days2003-03-18
\n", "
" ], "text/plain": [ " order_date delivery_date Delivery Time Dobro do Tempo\n", "ID \n", "1 1998-05-24 1999-02-05 257 days 1999-10-20\n", "2 1992-04-22 1998-03-06 2144 days 2004-01-18\n", "4 1991-02-10 1992-08-26 563 days 1994-03-12\n", "5 1992-07-21 1997-11-20 1948 days 2003-03-22\n", "7 1993-09-02 1998-06-10 1742 days 2003-03-18" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "shipping.head()" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [], "source": [ "mascara = shipping['Delivery Time'] > '365 days'" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
order_datedelivery_dateDelivery TimeDobro do Tempo
ID
21992-04-221998-03-062144 days2004-01-18
41991-02-101992-08-26563 days1994-03-12
51992-07-211997-11-201948 days2003-03-22
71993-09-021998-06-101742 days2003-03-18
91990-01-251994-10-021711 days1999-06-09
101992-02-231998-12-302502 days2005-11-05
111996-07-121997-07-14367 days1998-07-16
181995-06-181997-10-13848 days2000-02-08
201992-10-171998-10-062180 days2004-09-24
231992-05-301999-08-152633 days2006-10-30
261996-04-111998-05-04753 days2000-05-26
321990-01-201998-07-243107 days2007-01-25
331994-09-211996-10-12752 days1998-11-03
351993-09-101996-04-28961 days1998-12-15
361990-05-151994-02-141371 days1997-11-16
391990-03-261993-01-251036 days1995-11-27
411992-02-061996-05-101555 days2000-08-12
501991-05-031999-07-172997 days2007-09-30
521994-09-021997-05-14985 days2000-01-24
531995-11-291998-06-23937 days2001-01-15
\n", "
" ], "text/plain": [ " order_date delivery_date Delivery Time Dobro do Tempo\n", "ID \n", "2 1992-04-22 1998-03-06 2144 days 2004-01-18\n", "4 1991-02-10 1992-08-26 563 days 1994-03-12\n", "5 1992-07-21 1997-11-20 1948 days 2003-03-22\n", "7 1993-09-02 1998-06-10 1742 days 2003-03-18\n", "9 1990-01-25 1994-10-02 1711 days 1999-06-09\n", "10 1992-02-23 1998-12-30 2502 days 2005-11-05\n", "11 1996-07-12 1997-07-14 367 days 1998-07-16\n", "18 1995-06-18 1997-10-13 848 days 2000-02-08\n", "20 1992-10-17 1998-10-06 2180 days 2004-09-24\n", "23 1992-05-30 1999-08-15 2633 days 2006-10-30\n", "26 1996-04-11 1998-05-04 753 days 2000-05-26\n", "32 1990-01-20 1998-07-24 3107 days 2007-01-25\n", "33 1994-09-21 1996-10-12 752 days 1998-11-03\n", "35 1993-09-10 1996-04-28 961 days 1998-12-15\n", "36 1990-05-15 1994-02-14 1371 days 1997-11-16\n", "39 1990-03-26 1993-01-25 1036 days 1995-11-27\n", "41 1992-02-06 1996-05-10 1555 days 2000-08-12\n", "50 1991-05-03 1999-07-17 2997 days 2007-09-30\n", "52 1994-09-02 1997-05-14 985 days 2000-01-24\n", "53 1995-11-29 1998-06-23 937 days 2001-01-15" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "shipping[mascara].head(20)" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Timedelta('8 days 00:00:00')" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "shipping['Delivery Time'].min()" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Timedelta('3583 days 00:00:00')" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "shipping['Delivery Time'].max()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.4" } }, "nbformat": 4, "nbformat_minor": 2 }