whatstk._chat

Library objects.

Classes

BaseChat(df[, platform]) Base chat object.
class whatstk._chat.BaseChat(df, platform=None)[source]

Bases: object

Base chat object.

Attributes

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

Methods

from_source(**kwargs) Load chat.
merge(chat[, rename_users]) Merge current instance with chat.
rename_users(mapping) Rename users.
to_csv(filepath) Save chat as csv.
df

Chat as pandas.DataFrame.

See also

property df

Chat as DataFrame.

Returns:pandas.DataFrame
property end_date

Chat end date.

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

Load chat.

Parameters:kwargs – Specific to the child class.
Raises:NotImplementedError – Must be implemented in children.
merge(chat, rename_users=None)[source]

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)[source]

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)[source]

Save chat as csv.

Parameters:filepath (str) – Name of file.
property users

List with users.

Returns:list