whatstk._chat

Library objects.

Classes:

BaseChat(df[, platform])

Base chat object.

class whatstk._chat.BaseChat(df: DataFrame, platform: str | None = None)[source]

Bases: object

Base chat object.

df

Chat as pandas.DataFrame.

See also

Attributes:

df

Chat as DataFrame.

df_system

Chat as DataFrame.

end_date

Chat end date.

is_group

True if the chart is a group.

name

Name of the chat.

start_date

Chat starting date.

users

List with users.

Methods:

filter_dates([date_min, date_max])

Filter chat by date range.

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.

property df: DataFrame

Chat as DataFrame.

Returns:

pandas.DataFrame

property df_system: DataFrame

Chat as DataFrame.

Returns:

pandas.DataFrame

property end_date: str | datetime

Chat end date.

Returns:

datetime

filter_dates(date_min: str | datetime | None = None, date_max: str | datetime | None = None) BaseChat[source]

Filter chat by date range.

Parameters:
  • date_min (str, datetime, optional) – Minimum date.

  • date_max (str, datetime, optional) – Maximum date.

Returns:

BaseChat – Filtered chat.

classmethod from_source(**kwargs: Any) None[source]

Load chat.

Parameters:

kwargs – Specific to the child class.

Raises:

NotImplementedError – Must be implemented in children.

property is_group: bool

True if the chart is a group.

A chat is detected as a group if it has more than 2 users (including the ‘system’). Groups with one person will not be detected as groups.

Returns:

bool

merge(chat: BaseChat, rename_users: Dict[str, str] | None = None) BaseChat[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:

BaseChat – 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)
property name: str | None

Name of the chat.

Returns None if no name could be found. The name is extracted from the username of with the first system message in the chat.

Returns:

list

rename_users(mapping: Dict[str, str]) BaseChat[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: str | datetime

Chat starting date.

Returns:

datetime

to_csv(filepath: str) None[source]

Save chat as csv.

Parameters:

filepath (str) – Name of file.

property users: List[str]

List with users.

Returns:

list