AttributeError: 'str' object has no attribute 'date' - python-3.x

I have written code as below:
import datetime as dt
d = dt.date(2000, 1, 15)
But I am getting an error:
AttributeError Traceback (most recent call last)
<ipython-input-23-669287944c85> in <module>
1 # for loop - to convert float->str date to month-year format
2 rnum = 0
----> 3 d = dt.date(2000, 1, 15)
4 data_2["Date_F"] = d
5 for dt in data_2["strDate"]:
AttributeError: 'str' object has no attribute 'date'
I am using Jupyter. Please tell me how to resolve this error.

dont know why you are getting this. I am getting output like datetime.date(2000, 1, 15)
You may check your indentation or something.

Related

AttributeError: 'int' object has no attribute 'count' in dictionary

my question is that i am trying to get dictionary done in google colab but it is getting error of AttributeError: 'int' object has no attribute 'count' in dictionary again and again.
temp_dict = {"33": [temperatures.count(33)],"34": [temperatures.count(34)],"39": [temperatures.count(39)], "40": [temperatures.count(40)], "42": [temperatures.count(42)],"29": [temperatures.count(29)]}
this is my code line but it is not able to count in google colab.is there something missing.
From what I'm getting is that variable temperatures is not a list but the int
temperatures = [1,2,3,3,4]
print(temperatures.count(1)) # Returns 1
print(temperatures.count(3)) # Returns 2
print(temperatures.count(5)) # Returns 0
# Possible your code
temperatures = 33
print(temperatures.count(33)) # Returns error: AttributeError: 'int' object has no attribute 'count'
If you have python 3.11, it will highlight in which part your code error was made, for example
Traceback (most recent call last):
File "c:\Users\******\Desktop\nf\a.py", line 2, in <module>
print(temperatures.count(33))
^^^^^^^^^^^^^^^^^^
AttributeError: 'int' object has no attribute 'count'

'DeltaMergeBuilder' object has no attribute 'WhenNotMatchedInsert'

while doing merge with deltatable getting below error.
'DeltaMergeBuilder' object has no attribute 'WhenNotMatchedInsert'
from delta.tables import *
delta_df = DeltaTable.forPath(spark,"dbfs:/user/hive/warehouse/FileStore/tables/stream_write2")
delta_df.alias("t").merge(
df.alias("s"),
"target.empid=source.empid").whenMatchedUpdate(set =
{
"name":"source.name",
"city":"source.city",
"country":"source.country",
"contactno":"source.contactno"
}
).WhenNotMatchedInsert(Values =
{
"empid":"source.empid",
"name":"source.name",
"city":"source.city",
"country":"source.country",
"contactno":"source.contactno"
}
)
.execute()
error:
AttributeError: 'DeltaMergeBuilder' object has no attribute 'WhenNotMatchedInsert'
AttributeError Traceback (most recent call last)
<command-3810732791373279> in <cell line: 1>()
1 delta_df.alias("t").merge(
2 df.alias("s"),
3 "target.empid=source.empid").whenMatchedUpdate(set =
4 {
5 "name":"source.name",
AttributeError: 'DeltaMergeBuilder' object has no attribute 'WhenNotMatchedInsert'
Command took 0.21 seconds -- byat 1/5/2023, 6:09:03 PM on Cluster
I am working on upsert in delta table but getting below error.
The error is simple - your function name is using upper case W, but it should be lower-case: whenNotMatchedInsert

Using zoneinfo with pandas.date_range

I am trying to use zoneinfo instead of pytz. I am running into a problem using zoneinfo to initiate dates and passing it on to pd.date_range.
Below is an example of doing the exact same thing with pytz and with zoneinfo. But, while passing it to pd.date_range getting an error with the latter.
pytz example:
start_date = datetime(2021, 1, 1, 0, 0, 0)end_date = datetime(2024, 1, 1, 0, 0, 0) # exclusive end range
pt = pytz.timezone('Canada/Pacific')start_date = pt.localize(start_date)end_date = pt.localize(end_date)
pd.date_range(start_date, end_date-timedelta(days=1), freq='d')
zoneinfo example:
start_date1 = '2021-01-01 00:00:00
start_date1 = datetime.strptime(start_date1, '%Y-%m-%d %H:%M:%S').replace(microsecond=0, second=0, minute=0, tzinfo=ZoneInfo("America/Vancouver"))end_date1 = start_date1 + relativedelta(years=3)
pd.date_range(start_date1, end_date1-timedelta(days=1), freq='d')
Yet, when using zoneinfo I get the following error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
~/Documents/GitHub/virtual/lib/python3.9/site-packages/pandas/_libs/tslibs/timezones.pyx in pandas._libs.tslibs.timezones.get_dst_info()
AttributeError: 'NoneType' object has no attribute 'total_seconds'
Exception ignored in: 'pandas._libs.tslibs.tzconversion.tz_convert_from_utc_single'
Traceback (most recent call last):
File "pandas/_libs/tslibs/timezones.pyx", line 266, in pandas._libs.tslibs.timezones.get_dst_info
AttributeError: 'NoneType' object has no attribute 'total_seconds'
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
~/Documents/GitHub/virtual/lib/python3.9/site-packages/pandas/_libs/tslibs/timezones.pyx in pandas._libs.tslibs.timezones.get_dst_info()
AttributeError: 'NoneType' object has no attribute 'total_seconds'
Exception ignored in: 'pandas._libs.tslibs.tzconversion.tz_convert_from_utc_single'
Traceback (most recent call last):
File "pandas/_libs/tslibs/timezones.pyx", line 266, in pandas._libs.tslibs.timezones.get_dst_info
AttributeError: 'NoneType' object has no attribute 'total_seconds'
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/var/folders/vp/7ptlp5l934vdh1lvmpgk4qyc0000gn/T/ipykernel_67190/3566591779.py in <module>
5 end_date1 = start_date1 + relativedelta(years=3)
6
----> 7 pd.date_range(start_date1, end_date1-timedelta(days=1), freq='d')
8
9 # Because certain distributions will be a result of combined distributions,
~/Documents/GitHub/virtual/lib/python3.9/site-packages/pandas/core/indexes/datetimes.py in date_range(start, end, periods, freq, tz, normalize, name, closed, **kwargs)
1095 freq = "D"
1096
-> 1097 dtarr = DatetimeArray._generate_range(
1098 start=start,
1099 end=end,
~/Documents/GitHub/virtual/lib/python3.9/site-packages/pandas/core/arrays/datetimes.py in _generate_range(cls, start, end, periods, freq, tz, normalize, ambiguous, nonexistent, closed)
450
451 if tz is not None and index.tz is None:
--> 452 arr = tzconversion.tz_localize_to_utc(
453 index.asi8, tz, ambiguous=ambiguous, nonexistent=nonexistent
454 )
~/Documents/GitHub/virtual/lib/python3.9/site-packages/pandas/_libs/tslibs/tzconversion.pyx in pandas._libs.tslibs.tzconversion.tz_localize_to_utc()
~/Documents/GitHub/virtual/lib/python3.9/site-packages/pandas/_libs/tslibs/timezones.pyx in pandas._libs.tslibs.timezones.get_dst_info()
AttributeError: 'NoneType' object has no attribute 'total_seconds'
Testing the parameters:
start_date==start_date1
and
end_date==end_date1
Both tests result in True.
if understanding correctly you want to create a date range (1D freq) using ZoneInfo…if correct I see a few things going on with your code.
#1 When dealing with datetimes be sure the object is in the correct dtype. I believe datetime64 format will work better.
#2 From the provide code I don’t think ‘strptime’ or ‘replace’ are needed. To access "America/Vancouver" within ZoneInfo you can make it work if you parse start_date1 into years, months, days, hours and minutes.
#3 When start_date1 is parsed, you can add 3 to years (or another number) to create the end date.
The above will create a DatetimeIndex over the specified range.
Datetimes are always tricky. As always you can get to the same destination using different paths…this is just one of them.
start_date_str = '2021-01-01 00:00:00'
start_date_datetime64 = pd.to_datetime(start_date_str) # change dtype to datetime64
year = start_date_datetime64.year
month = start_date_datetime64.month
day = start_date_datetime64.day
hour = start_date_datetime64.hour
minute = start_date_datetime64.minute
start_date_formatted = dt.datetime(year, month, day, hour, minute, tzinfo=ZoneInfo("America/Vancouver"))
end_date_formatted = dt.datetime(year + 3, month, day, hour, minute, tzinfo=ZoneInfo("America/Vancouver"))
result = pd.date_range(start_date_formatted, end_date_formatted-pd.Timedelta(days=1), freq='d')
OUTPUT- DatetimeIndex, dtype='datetime64[ns, America/Vancouver]', length=1095, freq='D')
This error was a result of compatibility between the pandas version and the nbformat version. Once I updated both to the newest version, the code worked with no error.

Pandas Dataframe: I want to use round() while dividing columns, but I'm getting an error

TypeError: loop of ufunc does not support argument 0 of type float which has no callable rint method
state_trend['Percent Change'].round(2)
There's a bunch of floats and ints in that column. Any idea what the problem is?
The error message is a bit misleading here and actually has nothing to do with the zeros.
The quick answer is:
state_trend['Percent Change'].astype(float).round(2)
I managed to replicate the error when the series is of type object. For float format the round function works well. And in the object format case, you get the same error with or without zeros
So all this code works as expected:
import pandas as pd
>>> s1 = pd.Series([0, 1, 2]) / pd.Series([1,2,3])
>>> s2 = pd.Series([1, 1, 2]) / pd.Series([1,2,3])
>>> s1.round(2)
0 0.00
1 0.50
2 0.67
dtype: float64
>>> s2.round(2)
0 1.00
1 0.50
2 0.67
dtype: float64
But when you convert the series to object you get the rounding error:
>>> t1 = s1.astype(object)
>>> t2 = s2.astype(object)
>>> t1.round(2)
AttributeError: 'float' object has no attribute 'rint'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.8/site-packages/pandas/core/series.py", line 2218, in round
result = self._values.round(decimals)
TypeError: loop of ufunc does not support argument 0 of type float which has no callable rint method
And this is with and without the zero:
>>> t2.round(2)
AttributeError: 'float' object has no attribute 'rint'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.8/site-packages/pandas/core/series.py", line 2218, in round
result = self._values.round(decimals)
TypeError: loop of ufunc does not support argument 0 of type float which has no callable rint method
So I just converted to float before the rounding:
>>> t1.astype(float).round(2)
0 0.00
1 0.50
2 0.67
dtype: float64
>>>
A final question will be how did the state_trend['Percent Change'] become of type object... generally this happens if you had None values before or the first series that generated the percent where of type object themselves.

how to call diffenet attribute of a Pandas dataframeusing a variabe?

consider df is a pandas data frame with 10 different columns and 500 rows. user is asked to pick a column name which will be stored in var1.
I am trying to call the corresponding column to var1 and change the data type but I see an error.
is there anyway to solve this problem?
Regards,
var1=input('Enter the file name:').lower().capitalize()
df[var1]=df.var1.astype(float)
error:
'DataFrame' object has no attribute 'file_name'
The current approach you're taking - using df.var1 to reference the var1 column - has pandas searching literally for a column/attribute named var1. A correct way of accessing this column/attribute would be to use something like this df[var1], which will look for whatever is contained in var1. See the example below for more detail:
>>> import pandas as pd
>>> var1 = 'hello'
>>> df = pd.DataFrame({'hello': [1]})
>>> df
hello
0 1
>>> df.var1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/blacksite/Documents/envs/dsenv/lib/python3.6/site-packages/pandas/core/generic.py", line 5067, in __getattr__
return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute 'var1'
>>> df.hello
0 1
Name: hello, dtype: int64
>>> df[var1]
0 1
Name: hello, dtype: int64

Resources