WhatsAppChat

Object WhatsAppChat works as a bridge between the python code and the whatsapp chat text file. Easily load a chat from a text file and work with it using all the power of pandas.

A chat can be loaded from a single source file using WhatsAppChat.from_source

Concept diagram of WhatsAppChat.from_source

or multiple source files using WhatsAppChat.from_sources

Concept diagram of WhatsAppChat.from_sources
class whatstk.WhatsAppChat(df)[source]

Bases: whatstk._chat.BaseChat

Load and process a WhatsApp chat file.

Parameters:df (pandas.DataFrame) – Chat.

Attributes

df Chat as DataFrame.
end_date Chat end date.
start_date Chat starting date.
users List with users.

Methods

from_source(filepath, **kwargs) Create an instance from a chat text file.
from_sources(filepaths[, auto_header, …]) Load a WhatsAppChat instance from multiple sources.
merge(chat[, rename_users]) Merge current instance with chat.
rename_users(mapping) Rename users.
to_csv(filepath) Save chat as csv.
to_txt(filepath[, hformat, encoding]) Export chat to a text file.

Example

This simple example loads a chat using WhatsAppChat. Once loaded, we can access its attribute df, which contains the loaded chat as a DataFrame.

>>> from whatstk.whatsapp.objects import WhatsAppChat
>>> from whatstk.data import whatsapp_urls
>>> chat = WhatsAppChat.from_source(filepath=whatsapp_urls.POKEMON)
>>> chat.df.head(5)
                 date     username                                            message
0 2016-08-06 13:23:00  Ash Ketchum                                          Hey guys!
1 2016-08-06 13:25:00        Brock              Hey Ash, good to have a common group!
2 2016-08-06 13:30:00        Misty  Hey guys! Long time haven't heard anything fro...
3 2016-08-06 13:45:00  Ash Ketchum  Indeed. I think having a whatsapp group nowada...
4 2016-08-06 14:30:00        Misty                                          Definetly
property df

Chat as DataFrame.

Returns:pandas.DataFrame
property end_date

Chat end date.

Returns:datetime
classmethod from_source(filepath, **kwargs)[source]

Create an instance from a chat text file.

Parameters:
  • filepath (str) –

    Path to the file. Accepted sources are:

    • Local file, e.g. ‘path/to/file.txt’.
    • URL to a remote hosted file, e.g. ‘http://www.url.to/file.txt’.
    • Link to Google Drive file, e.g. ‘gdrive://35gKKrNk-i3t05zPLyH4_P1rPdOmKW9NZ’. The format is expected to be ‘gdrive://[FILE-ID]’. Note that in order to load a file from Google Drive you first need to run gdrive_init.
  • **kwargs – Refer to the docs from df_from_txt_whatsapp for details on additional arguments.
Returns:

WhatsAppChat – Class instance with loaded and parsed chat.

classmethod from_sources(filepaths, auto_header=None, hformat=None, encoding='utf-8')[source]

Load a WhatsAppChat instance from multiple sources.

Parameters:
  • filepaths (list) – List with filepaths.
  • auto_header (bool, optional) – Detect header automatically (applies to all files). If None, attempts to perform automatic header detection for all files. If False, hformat is required.
  • hformat (list, optional) – List with the header format to be used for each file. The list must be of length equal to len(filenames). A valid header format might be ‘[%y-%m-%d %H:%M:%S] - %name:’.
  • encoding (str) – Encoding to use for UTF when reading/writing (ex. ‘utf-8’). List of Python standard encodings.
Returns:

WhatsAppChat – Class instance with loaded and parsed chat.

Example

Load a chat using two text files. In this example, we use sample chats (available online, see urls in source code whatstk.data).

>>> from whatstk.whatsapp.objects import WhatsAppChat
>>> from whatstk.data import whatsapp_urls
>>> filepath_1 = whatsapp_urls.LOREM1
>>> filepath_2 = whatsapp_urls.LOREM2
>>> chat = WhatsAppChat.from_sources(filepaths=[filepath_1, filepath_2])
>>> chat.df.head(5)
                 date        username                                            message
0 2019-10-20 10:16:00            John        Laborum sed excepteur id eu cillum sunt ut.
1 2019-10-20 11:15:00            Mary  Ad aliquip reprehenderit proident est irure mo...
2 2019-10-20 12:16:00  +1 123 456 789  Nostrud adipiscing ex enim reprehenderit minim...
3 2019-10-20 12:57:00  +1 123 456 789  Deserunt proident laborum exercitation ex temp...
4 2019-10-20 17:28:00            John                Do ex dolor consequat tempor et ex.
merge(chat, rename_users=None)

Merge current instance with chat.

Parameters:
  • chat (WhatsAppChat) – Another chat.
  • rename_users (dict) – Dictionary mapping old names to new names. Example: {‘John’:[‘Jon’, ‘J’], ‘Ray’: [‘Raymond’]} will map ‘Jon’ and ‘J’ to ‘John’, and ‘Raymond’ to ‘Ray’. Note that old names must come as list (even if there is only one).
Returns:

WhatsAppChat – Merged chat.

Example

Merging two chats can become handy when you have exported a chat in different times with your phone and hence each exported file might contain data that is unique to that file.

In this example however, we merge files from different chats.

>>> from whatstk.whatsapp.objects import WhatsAppChat
>>> from whatstk.data import whatsapp_urls
>>> filepath_1 = whatsapp_urls.LOREM1
>>> filepath_2 = whatsapp_urls.LOREM2
>>> chat_1 = WhatsAppChat.from_source(filepath=filepath_1)
>>> chat_2 = WhatsAppChat.from_source(filepath=filepath_2)
>>> chat = chat_1.merge(chat_2)
rename_users(mapping)

Rename users.

This might be needed in multiple occations:

  • Change typos in user names stored in phone.
  • If a user appears multiple times with different usernames, group these under the same name (this might
    happen when multiple chats are merged).
Parameters:mapping (dict) – Dictionary mapping old names to new names, example: {‘John’: [‘Jon’, ‘J’], ‘Ray’: [‘Raymond’]} will map ‘Jon’ and ‘J’ to ‘John’, and ‘Raymond’ to ‘Ray’. Note that old names must come as list (even if there is only one).
Returns:pandas.DataFrame – DataFrame with users renamed according to mapping.
Raises:ValueError – Raised if mapping is not correct.

Examples

Load LOREM2 chat and rename users Maria and Maria2 to Mary.

>>> from whatstk.whatsapp.objects import WhatsAppChat
>>> from whatstk.data import whatsapp_urls
>>> chat = WhatsAppChat.from_source(filepath=whatsapp_urls.LOREM2)
>>> chat.users
['+1 123 456 789', 'Giuseppe', 'John', 'Maria', 'Maria2']
>>> chat = chat.rename_users(mapping={'Mary': ['Maria', 'Maria2']})
>>> chat.users
['+1 123 456 789', 'Giuseppe', 'John', 'Mary']
property start_date

Chat starting date.

Returns:datetime
to_csv(filepath)

Save chat as csv.

Parameters:filepath (str) – Name of file.
to_txt(filepath, hformat=None, encoding='utf8')[source]

Export chat to a text file.

Usefull to export the chat to different formats (i.e. using different hformats).

Parameters:
  • filepath (str) – Name of the file to export (must be a local path).
  • hformat (str, optional) – Header format. Defaults to ‘%y-%m-%d, %H:%M - %name:’.
  • encoding (str, optional) –

    Encoding to use for UTF when reading/writing (ex. ‘utf-8’). List of Python standard encodings.

property users

List with users.

Returns:list